我有这段代码:
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
我想知道另一种写这种方式,或者这是最有效的方法。我在考虑用户模型中的范围。有什么想法吗?
答案 0 :(得分:6)
我会将regular
和employee
写为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