I have several user types in my app. I use Devise for authentication. I started with just a
User model and that’s where all the Devise attributes are.
Later I added more user types and chose to use inheritance, so now I have something like this:
# user.rb class User << ActiveRecord::Base ... basic Devise stuff ... basic user methods like full_name etc end # owner.rb class Owner << User ... owner-specific associations ... extra methods/scopes specific to owners end # teacher.rb class Teacher << User ... teacher-specific associations ... extra methods/scopes specific to teachers end # family.rb class Family << User ... teacher-specific associations ... extra methods/scopes specific to families end
It worked fine for a while but, predictably, requirements arose for owners to have a
stripe_customer_id column, teachers to have a
lesson_template column and families to have
secondary_email column. More type-specific attributes are coming with future features so I’d like to improve this now rather than later.
I’ve been advised to do it instead with Composition but I haven’t quite grasped how. Could somebody show an example of how I might achieve it? And is it an easy move or am I too committed to this method to change it now?