Different User Types

rails && users && roles

Reply
Reply to all Reply to allForward Forward Print Add Chris to Contacts
list Delete this message Show original Message text garbled?
Chris K.
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
(
email
password
first_name
last_name
)

create table restaurateur
(
is_admin
is_owner
)

Rails STI won’t work because a user is not limited to a single
user_type.

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
end

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

class restaurateur
belongs_to :user
belongs_to :restaurant
end

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
end

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
self.user.name
end


end

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

class Restaurteur
include UserProxy
end

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.

So, I’m interested in what your thoughts are:

  1. This particular solution
  2. How to build the proxy so that I don’t have to define each
    method/attribute by hand within the proxy, i.e. how to copy the User
    definition to the UserProxy in the most efficient manner.

I’ve looked into delegate, Forwardable and missing_method. I can’t seem
to get the syntax right or I’m just missing some concept.

Any ideas, thoughts, code (especially code :slight_smile: … would be appreciated.

Cheers

On Dec 18, 7:01 pm, Chris K. [email protected]
wrote:

The base data for all users is the same:
(
class user
belongs_to :user
I want to clean the api up so that I can just do restaurateur.email.

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…

Any ideas, thoughts, code (especially code :slight_smile: … would be appreciated.

Cheers


Posted viahttp://www.ruby-forum.com/.


Ryan R.
http://raaum.org
http://rails.raaum.org – Rails docs
http://locomotive.raaum.org – Self contained Rails for Mac OS X