views:

32

answers:

2

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

A: 

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.

ajmurmann
STI = Single Table Inheritance
theIV
Thanks, I expected something fancier...
ajmurmann
Thanks for your answer. My intent was connections was to capture the "team" around the horse. It's my intent to also track/analyze changes in connections and the results thereof.
Mutuelinvestor
Update: I have quite figured it out yet, but I'm getting closer. While researching the topic I came across this article that I thought others might be interested in. http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html?dsq=41117690#comment-41117690
Mutuelinvestor
+1  A: 

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

Eimantas
Thanks. I will have a look at the document. Do you have any observations or insights you would be willing to share with me. Many thanks.
Mutuelinvestor
Eimantas, Thanks again for the document link. I just read through it as well as all the comments. I must confess I tend to side with those who don't think STI is evil. In my project, the non-STI approach would have resulted in Three tables with identical columns. It would also mean that I would have a lot more foreign key references throughout the project. That said, I very new to this and I appreciate your input. Anyone else care to offer an opinion or share some experience.
Mutuelinvestor