Ruby models, should I choose Inheritance or just associations
I have the following tables and fields
User :username,:password,:email Organiser :organiser_specific_fields Participant :participant_specific_fields
Orgnaisers and Participants are Users,
Users already exist in system,
When a User organises a Party, I create an instance of Party class, add him as Organiser of that Party and add other Users as Participants.
How do I model this in Rails? I couldn't figure out what's the best way to implement this in Rails considering that Users already exist in the system.
Based on you comments I suggest that you stick with the following schema:
- A user table, saving all the details about a user (firstname, lastname)
- A party table with all the details about a party (time, location, ...)
- Two join tables for the models outlined below:
This code is untested, it might need some tweaking of the names:
class User < ActiveRecord::Base has_many :parties, :through => :participations, :source => :user has_many :organised, :through => :organises, :source => :user end class Party < ActiveRecord::Base has_many :participants, :through => :participations has_many :organizers, :through => :organises end class Participation < ActiveRecord::Base belongs_to :user belongs_to :party # this class represents "a user is going to a party" # additional fields, which are specific to a participation go here as well end class Organises < ActiveRecord::Base belongs_to :user belongs_to :party # This class represents "user is the organisator of a party" # additional fields, which are specific to organizing a party go here end
- You don't need to touch your user tables
- A party can have two or more organisators
- The party's organisators can go to the party as well (hopefully)
- No data duplication in your database
- You have to manange the associations yourself: A user must not go to (or be the organisator) of the same party twice
I hope that helps to clear things up a bit. More info can be found in the rails docs at this link.