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.

Answers


Based on you comments I suggest that you stick with the following schema:

  1. A user table, saving all the details about a user (firstname, lastname)
  2. A party table with all the details about a party (time, location, ...)
  3. 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

Pros:

  • 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

Cons:

  • 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.


Need Your Help

Facebook sdk 3.0 with iOS 5 and Xcode 4.2

iphone ios xcode facebook

I have been making iOS apps for a while now and am pretty comfortable with that. What I haven't done is add Facebook connectivity in any of my apps.

Action Item shown only in Landscape?

android android-4.0 actionbarsherlock android-actionbar

I was just wondering if it were at all possible for an Action Bar icon to only be shown when the device is in landscape mode? In portrait I have an onscreen button for a certain task, but in order to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.