开发者

Virgin STI Help

开发者 https://www.devze.com 2022-12-23 21:28 出处:网络
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

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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号