rails 模型上的多对多关系
Many to many relation on a model with rails
如何使用 rails 实现此目的?
我想用相同的模型加入 table,它是给同事的,所以我想让员工 1 和员工 2 以及 1 和 3...等等
那么这是在活动记录中解决这个问题的最佳方法吗?
class Employee < ActiveRecords::Base
end
class Colleague < ActiveRecords::Base
has_many :employees, :foreign_key => 'employee_id'
has_many :colleague, :foreign_key => 'employee_id'
end
你怎么看?
我的迁移应该如何进行?
像这样?
create_table :colleague do |t|
t.integer :employee_id
t.integer :colleague_id
end
您可以使用 has_and_belongs_to_many
.
对于您的示例,这将类似于:
class Employee < ActiveRecord::Base
has_and_belongs_to_many :colleagues
end
class Colleague < ActiveRecord::Base
has_and_belongs_to_many :employees
end
也就是说,我想您真正想要的是员工成为自我参照模型,如所述here
我会用 has_many :through
关系来处理这个问题。首先,创建连接 table
class CreateColleagueships < ActiveRecord::Migration
def change
create_table :colleagueships do |t|
t.references :employee, index: true
t.references :colleague, index: true
t.timestamps null: false
end
end
end
设置关联
class Colleagueship < ActiveRecord::Base
belongs_to :employee
belongs_to :colleague, :class_name => 'Employee'
end
class Employee < ActiveRecord::Base
has_many :colleagueships
has_many :colleagues, :through => :colleagueships
# ...
end
然后您可以通过
“添加同事”
@colleagueship = employee.colleagueships.build(:colleague_id => params[:colleague_id])
@colleagueship.save
如果换一种思路,你可以这样做:
class Employee < ActiveRecord::Base
belongs_to :employer, class_name: Employee
has_many :employees, class_name: 'Employee', foreign_key: 'employer_id'
end
因此,除了雇主为 nil 的情况外,同事通常 self.employer.employees
,因此您必须编写一个简单的方法来处理这种情况,例如:
def colleagues
if employer.nil?
Employee.none
else
employer.employees
end
end
好处是,您不需要额外的表。
如何使用 rails 实现此目的? 我想用相同的模型加入 table,它是给同事的,所以我想让员工 1 和员工 2 以及 1 和 3...等等
那么这是在活动记录中解决这个问题的最佳方法吗?
class Employee < ActiveRecords::Base
end
class Colleague < ActiveRecords::Base
has_many :employees, :foreign_key => 'employee_id'
has_many :colleague, :foreign_key => 'employee_id'
end
你怎么看? 我的迁移应该如何进行?
像这样?
create_table :colleague do |t|
t.integer :employee_id
t.integer :colleague_id
end
您可以使用 has_and_belongs_to_many
.
对于您的示例,这将类似于:
class Employee < ActiveRecord::Base
has_and_belongs_to_many :colleagues
end
class Colleague < ActiveRecord::Base
has_and_belongs_to_many :employees
end
也就是说,我想您真正想要的是员工成为自我参照模型,如所述here
我会用 has_many :through
关系来处理这个问题。首先,创建连接 table
class CreateColleagueships < ActiveRecord::Migration
def change
create_table :colleagueships do |t|
t.references :employee, index: true
t.references :colleague, index: true
t.timestamps null: false
end
end
end
设置关联
class Colleagueship < ActiveRecord::Base
belongs_to :employee
belongs_to :colleague, :class_name => 'Employee'
end
class Employee < ActiveRecord::Base
has_many :colleagueships
has_many :colleagues, :through => :colleagueships
# ...
end
然后您可以通过
“添加同事”@colleagueship = employee.colleagueships.build(:colleague_id => params[:colleague_id])
@colleagueship.save
如果换一种思路,你可以这样做:
class Employee < ActiveRecord::Base
belongs_to :employer, class_name: Employee
has_many :employees, class_name: 'Employee', foreign_key: 'employer_id'
end
因此,除了雇主为 nil 的情况外,同事通常 self.employer.employees
,因此您必须编写一个简单的方法来处理这种情况,例如:
def colleagues
if employer.nil?
Employee.none
else
employer.employees
end
end
好处是,您不需要额外的表。