I am writing ruby on rails application, which will have 2 different users types (let’s say sellers and buyers). I am using devise gem to handle authentication.
I don’t want to use single table inheritance for 3 reasons:
1) I don’t want to have a lot of columns in my user table (I would have to store fields for both seller and buyer in one table, and they will have a lot of different fileds)
2) I don’t want to have a lot of null values in my database
3) Keeping sellers and buyers separated will help to improve performance speed
I decided to create 2 separate models for sellers and buyers (and generate devise for each of them). Long story short – it caused some troubles (ie. difficulties with handling login with single form, using polymorphic associations for simple things like “private message”, keeping my code DRY etc.)
So here is the question:
Do You think it would be good idea to create one model (user) with some common fields to easily handle authentication with devise and then have separate models (sellers and buyers) with belongs_to and has_one associations to store more info. Something like this:
class User < ActiveRecord::Base
has_one :seller
has_one :buyer
end
class Seller < ActiveRecord::Base
belongs_to :user, :conditions => "user_type= 'seller'"
end
class Buyer < ActiveRecord::Base
belongs_to :user, :conditions => "user_type= 'buyer'"
end
Is that a good solution? Or do You think single table inheritance would be a better idea?
What troubles / difficulties it may cause?
What will be routes for displaying profile page for each user? I would like to have something like website.com/sellers/id and website.com/buyers/id
If You can think of better solution for my problem – please let me know.
It would be even better if you create seperate tables for Seller and Buyer and use polymorphic association like there Inheritance and polymorphic-associations in rails.
Seperating these two things you will gain speed in DB queries and keeping the Seller and Buyer logic on their own classes also is very good practice.