Virgin STI Help

Posted by Mutuelinvestor on Stack Overflow See other posts from Stack Overflow or by Mutuelinvestor
Published on 2010-03-21T16:24:40Z Indexed on 2010/03/21 16:31 UTC
Read the original article Hit count: 334

I am working on a horse racing application and I'm trying to utilize STI to model a horse's connections. A horse's connections is comprised of his owner, trainer and jockey. Over time, connections can change for a variety of reasons:

  1. The horse is sold to another owner
  2. The owner switches trainers or jockey
  3. The horse is claimed by a new owner

As it stands now, I have model this with the following tables:

  1. horses
  2. connections (join table)
  3. stakeholders (stakeholder has three sub classes: jockey, trainer & owner)

Here are my clases and associations:

    class Horse < ActiveRecord::Base
    has_one :connection
    has_one :owner_stakeholder, :through => :connection
    has_one :jockey_stakeholder, :through => :connection
    has_one :trainer_stakeholder, :through => :connection
end

    class Connection < ActiveRecord::Base
    belongs_to :horse
    belongs_to :owner_stakeholder
    belongs_to :jockey_stakeholder
    belongs_to :trainer_stakeholder
end

class Stakeholder < ActiveRecord::Base
    has_many :connections
    has_many :horses, :through => :connections
end

class Owner < Stakeholder
  # Owner specific code goes here.
end

class Jockey < Stakeholder
  # Jockey specific code goes here.
end

class Trainer < Stakeholder
  # Trainer specific code goes here.
end

One the database end, I have inserted a Type column in the connections table.

Have I modeled this correctly. Is there a better/more elegant approach. Thanks in advance for you feedback.

Jim

© Stack Overflow or respective owner

Related posts about single-table-inheritance

Related posts about activerecord