views:

54

answers:

3

so I have these relationships:

a location:

has_many :services
has_many :products, :through => :services 

a product:

  has_many :services
  has_many :locations, :through => :services
  has_many :add_ons

and a service:

belongs_to :product
belongs_to :location

has_many :service_add_ons
has_many :add_ons, :through => :service_add_ons

a service_add_on:

belongs_to :service
belongs_to :add_on

How can I write a :through that will return a location with it's products and each products add_ons? so far I have:

wants.json { render :json => @location.to_json(:include => {:products => {:add_ons}}) }

which is obviously not working. What can I do to change this and make it work?

A: 

I believe you should use

:include => {:products => :add_ons}
j.
that didn't work, perhaps I should have incluced this:wants.json { render :json => @locations.to_json( :include => [:products => :add_ons] ) }... I added that to the question too
jtmkrueger
A: 

Sorry for my english :S

I think you have an error here:

a service_add_on:

belongs_to :service

belongs_to :add_on

add_on must belongs_to service_add_on and not contrary

el_quick
+1  A: 

Try this:

wants.json { 
  render :json => @location.to_json(:include => {:products => {:include => :add_ons}}) 
}
benr75
I didn't think to try and nest the include! It works perfectly though!
jtmkrueger