1

I'm trying to build a application that has different kinds of users, I'm using authlogic for user authentication.

So I have one user model that has the required field for authlogic to do its magic. I now want to add a couple of different models that would describe the extra fields for the different kinds of users.

Lets say that a user registers, he would then select his user type, when he is done registering he would be able to add information that is specific for his user model.

What would be the best way to do this? I am currently looking into polymorphic models but I'm not sure that's the best route to take. Any help would be greatly appreciated, thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pallinder
  • 255
  • 1
  • 6
  • 11

2 Answers2

6

You can create different profile tables and just tie the profile to the user. So for each user type you can create a table and store the specific info there and have a user_id column to point back to users.

class User < ActiveRecord::Base
  has_one :type_1
  has_one :type_2
end

class Type1 < ActiveRecord::Base
  belongs_to :user
end

class Type2 < ActiveRecord::Base
  belongs_to :user
end

Now this isn't very DRY and could lead to problems if you are constantly adding user types. So you could look into polymorphism.

For polymorphism, the users table would define what type the user is (profileable_id and profileable_type). So something like this:

class User < ActiveRecord::Base
  belongs_to :profileable, :polymorphic => true
end

class Type1 < ActiveRecord::Base
  has_one :user, :as => :profileable
end

class Type2 < ActiveRecord::Base
  has_one :user, :as => :profileable
end

Then there is a third option of STI (single table inheritance) for the user types. But that doesn't scale well if the user type fields differ dramatically.

Tony Fontenot
  • 5,091
  • 1
  • 21
  • 19
  • Thanks, thats pretty much what I did with regards to polymorphic and it seems to work allright, problem is though that I cant for the life of me figure out how to handle the pulldown where the user selects his user type, how do I link that so the models work allright? – Pallinder Apr 06 '10 at 19:12
  • That's a whole different question :) But I would do it manually in the controller. Have a `select_tag` with the available types then, in the create/update method, check for the param, create/update with the appropriate type and save. – Tony Fontenot Apr 06 '10 at 19:18
  • Thanks a bunch, I figured that was the way to do it but then I thought there might be one of those nice "railsy" ways to do it hehe – Pallinder Apr 06 '10 at 19:40
  • Thanks a lot for this answer. Though i have a question, if for example a user with Type 1 can upgrade to Type 2 and can downgrade back to Type 1, how can this be handled ? Currently i have decided to leave the row filled with the user info in Type 1, while the user is actively on Type 2, so that when the user comes back/downgrades to Type 1 he doesn't have to retype his info. Is this the correct to go about it – Ewomazino Ukah Apr 23 '17 at 01:33
1

The best approach I saw it here http://astockwell.com/blog/2014/03/polymorphic-associations-in-rails-4-devise/

Nesha Zoric
  • 6,218
  • 42
  • 34