How to model different users in Rails

Question

I have a User model with authorization and authentication logic.

Now I understand that I have three different types of users. I want to store various information about each of them.

What is the best way to handle this in Rails?

Thoughts Based on Current Reading

I looked at STI, but from what I read, I think this is inappropriate, because there will be many NULL fields in my database.

Ideally, I would not duplicate the authentication / authorization logic for each of the three types of users.

Each user will also have different functionality in the application.

+4
source share
2 answers

You can try to use polymorphic associations and create a users table with data that all types of users have and put other data in separate tables. Railscast episode covering this topic.

+1
source

There are many ways to do this. Here is one approach:

Instead of thinking about different types of users, you can think about the roles that the user has.

For example, if a user can be a master of a butcher, a baker, or a candlestick, you can have four tables: users , butchers , bakers , candlestick_makers . The last three role tables have a user_id column; they "belong" to the user.

If you need to ensure that a specific user has only one role, you will need to do this in the application (since this database schema allows multiple roles for one user).

This method is good if there are many things in these role tables. If not, leaving some NULL columns in the users table will probably not kill you.

0
source

All Articles