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

Mat*_*iby 0 scope ruby-on-rails ruby-on-rails-3

我有这个代码:

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
Run Code Online (Sandbox Code Playgroud)

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

tok*_*and 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
Run Code Online (Sandbox Code Playgroud)