我怎么得到这个:通过?

时间:2010-08-31 17:15:45

标签: mysql ruby-on-rails join

所以我有这些关系:

位置:

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}}) }

显然不起作用。我该怎么做才能改变它并让它发挥作用?

3 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:0)

我相信你应该使用

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

答案 2 :(得分:0)

对不起我的英文:S

我认为你在这里有错误:

service_add_on:

belongs_to :service

belongs_to :add_on

add_on必须为belongs_to service_add_on而不是相反