维珍 STI 帮助

发布于 2024-08-25 18:51:20 字数 1145 浏览 10 评论 0原文

我正在开发一个赛马应用程序,并尝试利用 STI 来模拟马的连接。马的关系由主人、驯马师和骑师组成。随着时间的推移,连接可能会因各种原因而发生变化:

  1. 马匹被卖给另一位主人
  2. 主人更换驯马师或骑师
  3. 马匹被新主人认领

按照目前的情况,我使用下表对此进行建模:

  1. 马匹
  2. 连接(连接表)
  3. 利益相关者(利益相关者有三个子类:骑师、训练员和所有者)

以下是我的类和关联:

    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:

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

Have I modeled this correctly. Is there a better/more elegant approach. Thanks in advance for you feedback.

Jim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-09-01 18:51:20

有关在 Rails 项目中使用 STI 的信息,请参阅本文档。关于连接 - 多态关联是你最好的选择。

Please consult this document on using STI in rails projects. Regarding connections - polymorphic association is your best bet.

耀眼的星火 2024-09-01 18:51:20

首先我得说,我不知道什么是性病。缩写代表什么?

我不明白为什么你需要连接模型。根据我对您的域的理解,您可以直接断开连接,不需要使用 :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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文