Rails: Multiple "types" of one model through related models?

Posted by neezer on Stack Overflow See other posts from Stack Overflow or by neezer
Published on 2010-05-26T19:17:25Z Indexed on 2010/05/26 19:21 UTC
Read the original article Hit count: 216

Filed under:
|

I have a User model in my app, which I would like to store basic user information, such as email address, first and last name, phone number, etc.

I also have many different types of users in my system, including sales agents, clients, guests, etc.

I would like to be able to use the same User model as a base for all the others, so that I don't have to include all the fields for all the related roles in one model, and can delegate as necessary (cutting down on duplicate database fields as well as providing easy mobility from changing one user of one type to another).

So, what I'd like is this:

User
-- first name
-- last name
-- email
--> is a "client", so
---- client field 1
---- client field 2
---- client field 3

User
-- first name
-- last name
-- email
--> is a "sales agent", so
---- sales agent field 1
---- sales agent field 2
---- sales agent field 3

and so on...

In addition, when a new user signs up, I want that new user to automatically be assigned the role of "client" (I'm talking about database fields here, not authorization, though I hope to eventually include this logic in my user authorization as well). I have a multi-step signup wizard I'm trying to build with wizardly. The first step is easy, since I'm simply calling the fields included in the base User model (such as first_name and email), but the second step is trickier since it should be calling in fields from the associated model (like--per my example above--the model client with fields client_field_1 or client_field_2, as if those fields were part of User).

Does that make sense? Let me know if that wasn't clear at all, and I'll try to explain it in a different way.

Can anyone help me with this? How would I do this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about model