错误有很多通过

时间:2014-02-12 07:50:15

标签: ruby-on-rails ruby has-many-through

我收到以下错误:

Cannot modify association 'Business#placements' because the source reflection class 'Placement' is associated to 'Employee' via :has_many.

我的模型如下:

class Employee < ActiveRecord::Base
  belongs_to :business
  has_many :votes, dependent: :destroy
  has_many :placements

class Placement < ActiveRecord::Base
  belongs_to :employee
  belongs_to :business

class Vote < ActiveRecord::Base
  belongs_to :employee

class Business < ActiveRecord::Base
  has_many :employees, dependent: :destroy
  has_many :placements, through: :employees

我打电话时发生错误

@employee.votes.create!(phone_id: 35435656)

我出错的任何想法?

4 个答案:

答案 0 :(得分:4)

从您的展示位置模型中删除belongs_to:business:

class Employee < ActiveRecord::Base
  belongs_to :business
  has_many :votes, dependent: :destroy
  has_many :placements

class Placement < ActiveRecord::Base
  belongs_to :employee

class Vote < ActiveRecord::Base
  belongs_to :employee

class Business < ActiveRecord::Base
  has_many :employees, dependent: :destroy
  has_many :placements, through: :employees

答案 1 :(得分:2)

您需要删除

belongs_to :business

来自展示位置

它通过员工协会属于业务,因此您只需拥有belongs_to员工。

答案 2 :(得分:0)

员工有多个展示位置,Placement属于Employee,Business通过Employee进行多次展示。因此,业务与通过员工的放置相关联,其中放置本身属于员工。因此,您只需要删除

belongs_to :business 
来自Placement的

答案 3 :(得分:0)

联合,

Business model has_many :placements, through: :employees

Employes belongs_to :business

所以我们不需要belongs_to:业务在Placement Model