0

我有这个代码:

class Company < ActiveRecord::Base
  has_many :users, :through => :company_users do 
    def regular
      where('regular_user = ?', true)
    end

    def employee
      where('regular_user = ?', false)
    end
  end

我想知道另一种写这个的方法,或者这是最有效的方法。我正在考虑用户模型中的范围。有任何想法吗?

4

1 回答 1

6

我会写regularemployee作为范围User

class Company < ActiveRecord::Base
  has_many :users, :through => :company_users 
end

class User < ActiveRecord::Base
  scope :regular, where(:regular_user => true)
  scope :employee, where(:regular_user => false)
end
于 2012-05-18T19:08:19.603 回答