devise and multiple “user” models

Welcome aboard Java guy =), I hope you’ll enjoy the Rails world.
Simply, to solve your issue you have 2 solutions:

  1. For each user create a table in the database and corresponding model.
  2. Create a single table in the database and for each user type create a model. This is called single table inheritance (STI).

Which one to choose?
It depends on the common attributes of the roles. If they are almost common (for example all have a name, email, mobile, …) and a few attributes are different, I highly recommend the STI solution.

How to do the STI?
1. Simply create the the devise user model and table using the command rails generate devise User
2. Add a column named type with string datatype to the user table in the database using a migration.
3. For each user type create a model (for example rails g model admin)
4. Make the Admin class inherits from user model

class Admin < User
end

That’s it you are done =) … Yupeee

To create an admin run the command Admin.create(...) where the dots is the admin attributes for example the email, name, …

I think this question could help you too

Leave a Comment