开发者

how do I get this :through?

开发者 https://www.devze.com 2023-01-13 17:42 出处:网络
so I have these relationships: a location: has_many :services has_many :products, :thro开发者_如何学编程ugh => :services

so I have these relationships:

a location:

has_many :services
has_many :products, :thro开发者_如何学编程ugh => :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?


Try this:

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


I believe you should use

:include => {:products => :add_ons}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消