我正在开发一个赛马应用程序,并试图利用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其中一个是数据库端,我在connections表中插入了Type列。
我把这个模型做对了吗。有没有更好/更优雅的方法?谢谢您的反馈。
吉姆
发布于 2010-03-21 18:46:42
请咨询本文件在rails项目中使用STI。关于连接-多态关联是最好的选择。
发布于 2010-03-21 18:16:11
首先,我不得不说,我不知道什么是STI。这个缩写代表什么?
我不明白你为什么需要连接模型。据我对您的域的理解,您可以不使用连接,而不需要使用: To。这将使它更简单,并提高性能。我看不到连接模型添加的任何功能。
https://stackoverflow.com/questions/2487778
复制相似问题