2010-08-31 34 views
0

所以我有這些關係:我如何得到這個:通過?

位置:

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

產品:

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

和服務:

belongs_to :product 
belongs_to :location 

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

一個service_add_on:

belongs_to :service 
belongs_to :add_on 

我該怎麼寫:通過它將返回一個位置,它的產品和每個產品的add_ons? 迄今爲止我有:

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

這顯然不工作。我能做些什麼來改變這一點,並使其工作?

+0

你的產品模型是怎麼樣的? – 2010-08-31 17:20:59

+0

has_many:services has_many:locations,:through =>:services has_many:add_ons ...我將它添加到 – thatmiddleway 2010-08-31 17:45:42

回答

2

試試這個:

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

我沒有想過要嘗試嵌套include!它雖然完美,但! – thatmiddleway 2010-08-31 22:51:08

0

我相信你應該使用

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

這個問題上,但是沒有工作,或許我應該包括這個: wants.json {render:json => @ locations.to_json(:include => [:products =>:add_ons])} ...我還在這個問題中加入了 – thatmiddleway 2010-08-31 18:02:31

0

對不起,我的英語:S

我覺得你這裏有一個錯誤:

一個service_add_on:

belongs_to :service 

belongs_to :add_on 

ADD_ON必須belongs_to service_add_on而不是相反