2

I am using the popular Rabl gem for producing a JSON API in Rails. However, I am having some difficulty understanding how to return results of a HABTM association. I have a model called Sale and a model called Merchandise. A HABTM association is specified for each. My .rabl file looks like the following.

object @sales

attributes :name, :date_start, :date_end

child :merchandises do
    attributes :name
end

My model is simply passing @sales = Sale.all.

Nothing at all is returned in the child, even though many rows have valid relationships. Is there something else I need to do?

alpheus
  • 979
  • 6
  • 15
  • 34

1 Answers1

2

Try checking out https://github.com/nesquena/rabl/issues/149 issue. Perhaps do:

collection @sales
attributes :name, :date_start, :date_end
node :merchandises do |s|
  s.merchandises.to_a.map { |m| { :name => m.name } }
end

and see if that works any better?

Nathan
  • 1,381
  • 20
  • 43
  • 58