I have something bother me in the understanding of polymorphism (vs role):
Note: I am using rails (but it's a general question)
I have 4 models:
- User
- Pro
- Customer
- Company
There is a polymorphic association (profileable) between Pro/Customer and User [because Pro has many more fields for the registration process than Customer].
User is used for the session (I am using the devise gem)
Basically something very similar to this writing is used:
http://jeffsaracco.com/blog/2012/03/04/ruby-on-rails-polymorphic-user-model/
Company association
- Now, I want to create my association with Company.
- By now, only Pro has one Company (but I figure it could be other profileable_type which can also have a Company).
- Customer doesn't have a Company.
In that case which association is preferable:
Approach 1
- User has_one Company
- Company belongs_to User
- and manage authorization (with a gem like cancan) to permit company creation only for Pro (with my profileable_type field in the user table)
or:
Approach 2
- Pro has_one Company
- Company belongs_to Pro
- So in that case I don't need any authorization management anymore (User (and by extension Customer) cannot create company by default)
Looking for pros/cons of the 2 approaches. I am pretty sure the first one is the best solution as I have a user_id in my company table which would be more generic and expandable than having a pro_id.. I would be pleased to be sure. Thanks!