【问题标题】:how do I get this :through?我怎么得到这个:通过?
【发布时间】:2010-08-31 17:15:45
【问题描述】:

所以我有这些关系:

地点:

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

我如何编写一个 :through 来返回一个包含其产品和每个产品附加组件的位置? 到目前为止我有:

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

这显然是行不通的。我可以做些什么来改变它并让它发挥作用?

【问题讨论】:

  • has_many :services has_many :locations, :through => :services has_many :add_ons... 我将其添加到问题中

标签: mysql ruby-on-rails join


【解决方案1】:

试试这个:

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

【讨论】:

  • 我没想过要嵌套包含!不过效果很好!
【解决方案2】:

我相信你应该使用

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

【讨论】:

  • 那没用,也许我应该包括这个:wants.json { render :json => @locations.to_json( :include => [:products => :add_ons] ) }。 ..我也将其添加到问题中
【解决方案3】:

对不起我的英语:S

我认为你这里有一个错误:

一个 service_add_on:

belongs_to :service

belongs_to :add_on

add_on 必须属于_to service_add_on 而不是相反

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 2016-10-27
    • 2018-10-15
    • 2021-08-16
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多