0

I have a user model and a dealer and customer model, which inherit the user class and have no own database table:

class User < ActiveRecord::Base
end

class Dealer < User
  before_save :set_default_values

  def set_default_values
    self.role_id = Role.find_by_handle('dealer').id
  end
end

class Customer < User
    …
end

I don´t use scopes because i want something like a dealer_path and some custom logic per role.

Now i´m wondering how to tell my dealer/customer model that it´s role-dependent, so that i can use (for example) Customer.all and just get users with the role 'customer'.

Can someone point me in the right direction?

Arne Cordes
  • 581
  • 1
  • 6
  • 22

2 Answers2

0

You can use Rails' built-in Single Table Inheritance feature. Just add a column called 'type' to User, and set the value to 'dealer', 'customer', etc.

Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27
  • Hmm yes, but i have a role model and storing the role of a user as role_id. So i would need something like STI with role_id:integer instead of type:string. – Arne Cordes Aug 11 '12 at 11:48
0

Can't you just apply default scopes to your new classes?

class Dealer < User
  default_scope { where(:role_id) = Role.find_by_handle('dealer').id }
  ...
end
Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82