维珍 STI 帮助
我正在开发一个赛马应用程序,并尝试利用 STI 来模拟马的连接。马的关系由主人、驯马师和骑师组成。随着时间的推移,连接可能会因各种原因而发生变化:
- 马匹被卖给另一位主人
- 主人更换驯马师或骑师
- 马匹被新主人认领
按照目前的情况,我使用下表对此进行建模:
- 马匹
- 连接(连接表)
- 利益相关者(利益相关者有三个子类:骑师、训练员和所有者)
以下是我的类和关联:
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
在数据库端,我在连接表中插入了一个类型列。
我是否正确建模了?有没有更好/更优雅的方法。预先感谢您的反馈。
吉姆
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 connections table.
Have I modeled this correctly. Is there a better/more elegant approach. Thanks in advance for you feedback.
Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关在 Rails 项目中使用 STI 的信息,请参阅本文档。关于连接 - 多态关联是你最好的选择。
Please consult this document on using STI in rails projects. Regarding connections - polymorphic association is your best bet.
首先我得说,我不知道什么是性病。缩写代表什么?
我不明白为什么你需要连接模型。根据我对您的域的理解,您可以直接断开连接,不需要使用 :through 。这将使事情变得更简单并提高性能。我没有看到连接模型添加的任何功能。
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.