has_many通过与基于关系表的条件的关系(Rails 2.3.5)

时间:2012-06-14 17:50:50

标签: ruby-on-rails

我的关系如下。

companies_employee.rb
belongs_to :employee
belongs_to :company
validates_presence_of :role

employee.rb
has_many :companies_employees
has_many :companies, :through => :companies_employees

company.rb
has_many :companies_employees
has_many :managers, :through => :companies_employees, :source => :employee, conditions => {:role => "Manager"}
has_many :owners, :through => :companies_employees, :source => :employee, :conditions => {:role => "Owner"}

我的问题是,当它检查条件时,它会尝试在employees表中找到角色列,但是role列位于companies_employees表中。 有没有办法让它在这个表格中使用条件?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

has_many :managers, :through => :companies_employees, :source => :employee, conditions => ["employees.role = 'Manager"]
has_many :owners, :through => :companies_employees, :source => :employee, conditions => ["employees.role = 'Owner"]