← Back to Upcase

Adding profiles to several different user types all inheriting from Devise User class


(Dan Weaver) #1

I’m using Devise and its default User class for authentication and common information like name, etc.

I have 4 user types: Owner, Admin, Teacher and Student, each inheriting from User:

class AdminsController < UsersController

... # custom methods for admins

end

I need to add different attributes for each user types; some users have avatars, teachers have substitute boolean, students have age, etc.

Since all the users are in the User table with a type column, how do I go about adding specific attributes to different user types?

Have I made a problem for myself doing it this way? Should I be thinking of separate models for each user type?


(Jerry Busser) #2

If you are using Postgres, this is an option:

https://www.devmynd.com/blog/2013-3-single-table-inheritance-hstore-lovely-combination

I haven’t used this myself and am not necessarily endorsing it, but if you find something dirty about creating a bunch of null able columns on your users table, the is another way to go about it.

Any future named scopes or Active Record queries might get weird.

Have you completely ruled out composition over inheritance? Will a user be of only one type? Will a teacher never also be a student or an admin?


(Dan Weaver) #3

Thanks @Jerry_Busser, I got totally sidetracked on this. I’m back on it now so I’ll look into hstore.

I read some about composition but couldn’t figure out bow it would directly apply to this case. Do you hacve any favourite resources where I can learn more about it?