Question

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

Was it helpful?

Solution

Please consult this document on using STI in rails projects. Regarding connections - polymorphic association is your best bet.

OTHER TIPS

First I have to say, I don't know what STI is. What does the abbreviation stand for?

I don't understand why you need the connection-model. To my understanding of your domain, you could just leave connection away and wouldn't need to use :through. This would make it simpler and improve performance. I don't see any functionality the connection model adds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top