还有另一种方法可以在Ruby on Rails中编写它吗?

时间:2012-05-18 19:03:33

标签: ruby-on-rails ruby-on-rails-3 scope

我有这段代码:

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

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

1 个答案:

答案 0 :(得分: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