Search code examples
ruby-on-railsmodelpolymorphic-associationsmixinssti

RoR: STI / MTI / Mixin confusion


i have a problem which is i believe basic for most of the RoR developers. In fact, it's more about "understanding" than really programming it (i have already been programming a few modules to be able to extend ActiveRecord::Base to allow easy generation of Ext-JS related things without any problem).

Let's take a simple example, i'll talk in an ORM oriented language rather than a database / model language.

  • ThirdParty is a basic entity, having a common member named 'label'.
  • A ThirdParty instance can be a Customer, a Supplier, both, or none of them (So Customer / Supplier are like concrete interfaces rather than inherited classes).
  • Both Customer and Supplier have specialized members, which are for most of them unique to each respective class (Customer / Supplier don't share much data except the label field they are bound to via ThirdParty).

i have tried to do various things:

  • ThirdParty < ActiveRecord::Base
  • Customer < ThirdParty
  • Supplier < ThirdParty

[EDIT] Missed my explanation here. i meant: one single instance of ThirdParty could be a customer, a supplier, BOTH, OR NONE. As the first commenter pointed out, my example might not be the best... You can replace "ThirdParty" by "Character", and "Customer" / "Supplier" by "Archer" / "SwordsMan". A character can either be one, two, both, or none, but would provide the information on any instance of Character. The getters on Archer / SwordsMan shall return instances of Character "implementing" the interface contract, rather than just returning an instance of themself with a "getter" on the character object. [/EDIT]

Also i have tried:

  • ThirdParty < ActiveRecord::Base (has_one Customer; has_one Supplier)
  • Customer < ActiveRecord::Base (belongs_to ThirdParty)
  • Supplier < ActiveRecord::Base (belongs_to ThirdParty)

But it seems a bit "ugly" to me, because i have to do Customer.find(something).third_party.label to access data "inherited" by my "interfaces".

Also, i could include a customer_id and a supplier_id field on my ThirdParty model, but it doesn't seem right neither, because one ThirdParty can be both, or none of them...

And of course, i'd like to be able to find a solution which isn't obstructive / too much of a hack, to allow me for instance to then do some polymorphic association for another model (Such as an addressable, or commentable, contactable, etc...)

i also have been reading a lot on the subject like on such blog: http://mediumexposure.com/multiple-table-inheritance-active-record/ ... But i'am lost in the right way to do it (On most of the documentation i have found on the subject isn't clear if it's for Rails 1, 2, 3... What's recently been implemented on the matter, etc).

Can someone provide me with various "up-to-date" websites or to an example fitting the example i have given?

By advance, i thank you all for having red my question, and to edventually explain me the different techniques used regarding this particular subject.

P.S.: If you need me to explain more what i'm looking for, i can try... But keep in mind that my english is a bit "limited" on this domain, since i'm relatively new to RoR.


Solution

  • Having only one field in common, single (or multiple) table inheritance probably isn't the way to go. If you want to share functionality between the classes I would create a ThirdParty module that gets included into each class:

    module ThirdParty
      def self.included(base)
        base.extend ClassMethods
      end
    
      module ClassMethods
        # define your shared class methods here
      end
    
      # define you shared instance methods down here
    end
    
    class Customer < ActiveRecord::Base
      include ThirdParty
    end
    
    class Supplier < ActiveRecord::Base
      include ThirdParty
    end
    

    This allows you to define methods that utilize the label attribute of each including class. You mention that you'd like to be able to interface with other models, possibly polymorphically. If these associations are present in all ThirdParty objects then you would just setup the associations in either the self.included call (using instance_eval) or in ClassMethods as a method:

    module ThirdParty
      def self.included(base)
        base.extend ClassMethods
        base.instance_eval do
          has_many :buyers, :as => :third_party
        end
      end
    
      module ClassMethods
        def has_address
          has_one :address, :as => :third_party
          delegate :street, :city, :state, :zip, :to => :address
        end
      end
    end
    
    class Supplier < ActiveRecord::Base
      include ThirdParty # this would set up the :buyers association
    
      has_address # this sets up the address association
    end
    

    Hopefully that helps. For more information on doing stuff like this just google "ruby mixin" - there's ton of info out there.