Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Is there another way to write this in Ruby on Rails?

I have this code:

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

and I want to know of another way to write this, or if this the most effecient way. I was thinking of a scope in the user model. Any ideas?

Upvotes: 0

Views: 80

Answers (1)

tokland
tokland

Reputation: 67850

I would write regular and employee as scopes of 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

Upvotes: 5

Related Questions