Unique IDs between Users and Admins with Devise Rails

前端 未结 1 1806
一整个雨季
一整个雨季 2020-12-28 11:05

I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role

No

相关标签:
1条回答
  • 2020-12-28 11:30

    I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.

    class NormalUser < User
    end
    
    class Admin < User
    end
    

    The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.

    To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser

    0 讨论(0)
提交回复
热议问题