views:

151

answers:

2

Scenario: I have a users table in my application. I also have two subclasses of users, lets say contributors and viewers. Each user group must have an entirely different set of attributes but they both share certain user properties (login, password, name).

What is the best way to implement this using Ruby on Rails?

  • I think single table inheritance would leave me with too many null fields.
  • I think linking three tables (users, viewers, contributors) would work fine, but then when wanting to edit any information i have to do: @user.viewer, while i would love to be able to just do @viewer.

Any ideas of the best solution?

+1  A: 

don't compromise the database schema, make it fit best. I like the three table method. If you do the database bad, the application will have very hard to fix issues later, run slow, etc.

KM
+2  A: 

I would probably go with the three tables approach. Data integrity is king over code cleanliness.

If you want to make it look neater, put virtual attributes on the Viewer and Contributor models that make it look like the User attributes are local. You can make it a module and include it in both Viewer and Contributor models.

You can also set up an :include => :user on the default finders so that you don't get an extra query when using those fields.

I'm extremely caffeinated right now, so comment back if that doesn't make sense :)

zaius
haha, i am a little confused, i am def a rookie when it comes to rails. Is there a good resource on how to set up a module with with virtual attributes?
Sam