Different User Types

rails && users && roles

Reply to all Reply to allForward Forward Print Add Chris to Contacts
list Delete this message Show original Message text garbled?
  Chris Kilmer
to Lee

show details
   11:58 am (3½ hours ago)

Okay, I've been thing about this whole user/role business with Rails.

Simple scenario:
A restaurateur has multiple restaurants
A restaurant has multiple restauranteurs
A restaurant has multiple chefs

Both restaurateurs and chefs can be any other kind of user as well, i.e.
'marketer, member'

The base data for all users is the same:

create table user

create table restaurateur

Rails STI won't work because a user is not limited to a single

So, has_many :through seems the way to go. However, I want concrete
models for each user type, i.e. Restaurateur, Member, Chef, etc.

class user
  has_many :restaurateurs
  has_many :restaurants, :through => :restaurateurs

class restaurant
  has_many :restaurateurs
  has_many :users, :through => :restaurateurs

class restaurateur
  belongs_to :user
  belongs_to :restaurant

With this setup, if I wanted to iterate through each restaurateur and
print out the email address, I would have to do something like

restaurant.restaurateurs.each do
  puts restaurateur.user.email

I want to clean the api up so that I can just do restaurateur.email.

My thought was to create a UserProxy module:

module UserProxy
  def name


and then include the proxy in each user based class, i.e.

class Restaurteur
  include UserProxy

This cleans up the api significantly and allows the proxy to be used
between all user types.

Of course, if the user class changes, so does the proxy, which is a big
pain in the ass for maintenance.

Well, if the user class changes, updating the proxy is the least of
your concerns. You'll also have to update the associations in every
class, change the db column names or add the :class_name option to all
your associations, and so on. So I'm not sure that's a good strike
against your current solution.

I haven't really thought it through in detail, but have you considered
a "Role" model and join table. A user has many roles (owner, chef,
marketer, etc.). You can hook up the associations with using the
foreign_key and conditions options, _and_ it gives you some flexibility
to add more roles in the future without necessarily adding more tables
and classes...