ruby on rails - Make RoR nested representable into an array -
in using representable gem, looking nest object within array, array having 1 object. can't see figure out how accomplish this. ideas? reason due how google tag manager handles enhanced ecommerce tracking.
nested 'products' property :name, getter: lambda{|*| name } property :id end
will spit out:
"products": { "name" : "nike swoosh", "id" : 8 }
when want come out as:
"products": [ { "name" : "nike swoosh", "id" : 8 } ]
you can use collection in representable module. although have single object, since want wrap in array. technically become collection single object. define getter collection name in parent object. here example:
require 'ostruct' require 'representable' require 'representable/json' class order < struct.new(:order, :product) def products [product] end end module orderrepresenter include representable::json property :name collection :products end sale = order.new(name: "an order name", product: {name: "my product", id: 1}) p sale.extend(salerepresenter).to_json
Comments
Post a Comment