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:
- The horse is sold to another owner
- The owner switches trainers or jockey
- The horse is claimed by a new owner
As it stands now, I have model this with the following tables:
- horses
- connections (join table)
- 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 conn开发者_如何学编程ections table.
Have I modeled this correctly. Is there a better/more elegant approach. Thanks in advance for you feedback.
Jim
Please consult this document on using STI in rails projects. Regarding connections - polymorphic association is your best bet.
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.
精彩评论