STI - Single Table Inheritance is what you are looking for.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#class-ActiveRecord::Base-label-Single+table+inheritance
http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html
You create your model as always but you add an attribute even_type
as a string to your database. (default ist "type")
You let the EventModel know, that this will be the inheritance column.
class Event < ActiveRecord::Base
self.inheritance_column = :event_type
# dont forget your scopes to easy access them by type
# Event.party or Event.ultrafestival
scope :party, -> { where(event_type: 'Party') }
scope :ultrafestival, -> { where(event_type: 'UltraFestival') }
scope :tomorrowland, -> { where(event_type: 'TomorrowLand') }
def host
raise "needs to be implemented in your sub-class!"
end
end
Than you create some subclasses. Make sure those are inheriting from Event
class Party < Event
end
class UltraFestival < Event
end
class Tomorrowland < Event
end
Basically, all Objects are Events! So if you go Event.all
you'll get them all! Technically an Object can be something else. All those "Events" will be stored in the same table, they will be differed by the event_type
which will be in this example "party", "ultra_festival" or "tomorrowland".
Now you can add some special stuff to each of those classes for example
class Party < Event
def host
"private homeparty PTY"
end
def publish_photostream
end
def call_a_cleaning_woman
end
end
class UltraFestival < Event
attr_accessor :location
def host
"UMF Festival Corp."
end
def is_in_europe?
end
def is_in_asia?
end
end
class Tomorrowland < Event
def host
"ID&T"
end
def download_after_movie!
end
end
This is the standard Rails way - since years. Of course its working on every hoster and with postgres.
// edit:
if your sub-events need some special tables in the database, then you need to go MTI, multi-table-inheritance.