Of STI, MTI, or CTI, which is the currently suggested Rails 4 solution?
Asked Answered
C

1

7

I have the a basic events table, and want to have sub-tables for each event type (hiking, party, riverrun, etc).

I see a lot of old (2011/2012) posts regarding CTI, MTI and STI. Some solutions worked for Heroku, while others did not.

What is the "current" Rails way of doing this type of thing? Has this been added to Rails 4.x? Is there a magical Gem that handles this (with Postgres on Heroku)?

Some information if it helps:

In the future, there will be between 20-50 events, and each sub-table might be as many as 80 columns. The site is hosted on Heroku. Running Rails 4.0.2

Clino answered 17/6, 2014 at 15:12 Comment(1)
maybe give me some feedbeck on my answer to let me know if you need further assistenceInvite
I
0

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.

Invite answered 19/8, 2015 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.