Combining STI + polymorphism + rich self HABTM : how to?


#1

Hi all,

I need to model simple typed polymorphic relations:

'John'  ['works_for'] 'Bill'
'John' ['speaks_for'] 'Microsoft'
'Microsoft' ['hold_shares_of'] 'Apple'

I’m having a hard time combining :

  • S.T.I. : ‘people’ and ‘companies’ are different kind of
    ‘entities’ that can relate to other ‘entities’
  • polymorphism self-ref HABTM (a person is related to another person
    OR to a company)
  • rich HABTM : relations are typed (‘can_speak_for’,
    ‘works_for’, ‘works_with’, ‘knows’, …)

I have already used those techniques separatedly, but combining them is
another story!!

What I have :


STI models:

The main actors are People and Companies.
They have a lot of common data and represent a common concept: Entity

class Entity < ActiveRecord::Base
class Person < Entity
class Company < Entity

table “relations”

of_id :integer(11) = the left party: john, MSFT

of_type :string(20)

with_id :integer(11) = the right party…

with_type :string(20)

relation_type_id :integer(11) = L.U.T: “works_for”, “knows”

relationship models

class Relation < AR::B
class RelationType < AR::B

What I don’t know:


How to associate all those pieces?
(belongs_to, has_many, :as => …, :foreign_key, …)

I’m lost. Help. 911
Thanks in advance.

Alain