devise and multiple “user” models

前端 未结 4 1655
[愿得一人]
[愿得一人] 2020-11-27 09:21

I\'m using rails 3.2 and devise 2.0 and I\'m quite new to Rails.

Requirements

I\'d like to achieve the following:

  • have 2 or more \"user\" mod
4条回答
  •  独厮守ぢ
    2020-11-27 09:37

    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

提交回复
热议问题