【发布时间】:2019-09-12 19:33:08
【问题描述】:
我有 3 个模型:Company、Portable 和 Jobsite。我希望能够致电company.portables、company.jobsites、portable.company、portable.jobsite、jobsite.portables 和 jobsite.company。我已经像这样设置了我的关联:
class Company
has_many :portables
has_many :jobsites, through: :portables
end
class Portable
belongs_to :company
belongs_to :jobsite
end
class Jobsite
has_many :portables
has_one :company, through: :portables
end
除了jobsite.company,我可以成功调用所有的东西。当我拨打这个电话时,我得到:
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'Jobsite#company' where the :through association 'Jobsite#portables' is a collection. Specify a has_one or belongs_to association in the :through option instead.
设置这些关联的正确方法是什么?我是否必须在工作现场将工作现场添加到公司协会belongs_to :company 并将工作现场_id 添加到公司?似乎应该有另一种方法可以用我已经设置的方法来实现这一点。
【问题讨论】:
标签: ruby-on-rails rails-activerecord active-record-query