我正在开发一个赛马应用程序,并且正在尝试利用 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
在数据库端,我在连接表中插入了一个类型列。
我是否正确地建模了这个。有没有更好/更优雅的方法。提前感谢您的反馈。
吉姆