主动记录:我可以在没有实际多态性的情况下创建类似多态性的关联吗?

发布于 2025-01-26 14:12:41 字数 1432 浏览 3 评论 0原文

我不确定该如何说出这个问题(欢迎编辑),但这是我的问题:说我正在为现有结构添加到现有People表格,高级表表现类似:

  • 有很多
  • a 可以有几种不同类型的转运者,即。 Transporter ::滑板Transporter :: Car,等等。
    • 每个Transporter可能会有两个有用的列:
      • 外键person_id
      • 值列(将用于计算班级中的“质量”)(即model for transporter :: Carwheels wheels 用于运输::滑板
    • 可能只有3-5种运输商
  • 我唯一打算使用这些Transporter模型是:
    • SQL分析
    • 基于其运输者的“质量”来获取People的列表(基于其value列,请参见下文)

结构:

class Person < MainRecord
  has_many :transporters
end

module Transporter
  class Car < MainRecord

    def quality
      if car.model = 'Honda' # This is made up, heh
        return 'high_quality'
      else
        return 'low_quality'
      end
    end

  end
end

module Transporter
  class Skateboard < MainRecord

    def quality
      if skateboard.wheels = '4'
        return 'high_quality'
      else
        return 'low_quality'
      end
    end

  end
end

主要关注点:

  • 分析易于
  • 查询效率(抓取People在后端中带有“质量”转运器而没有太多麻烦)

我可以使用多态性,但是鉴于这些表大多将用于分析,因此中介多态性表可以是一个烦人的额外步骤。我还想到有一个带有多个列的运输桌,但我认为这不是最好的方法。

没有额外的中介表没有办法设置“多态性”?还是为此设置有更好的解决方案?

I was not sure how to word the question (edits welcome), but here is my question: Say I am adding a Transporters structure to existing People tables that high-level should act like:

  • There are many People
  • A Person can have a couple different types of Transporters, ie. Transporter::Skateboard, Transporter::Car, etc.
    • Each Transporter will probably have two useful columns:
      • the foreign key person_id
      • a value column (which will be used to calculate 'quality' in the class) (ie. model for Transporter::Car or wheels for Transported::Skateboard)
    • There will likely only ever be 3-5 types of Transporters
  • The only was I plan to use these Transporter models are:
    • SQL analysis
    • Grabbing a list of People based on the 'quality' of their transporter (which is based on its value column, see below)

Structure:

class Person < MainRecord
  has_many :transporters
end

module Transporter
  class Car < MainRecord

    def quality
      if car.model = 'Honda' # This is made up, heh
        return 'high_quality'
      else
        return 'low_quality'
      end
    end

  end
end

module Transporter
  class Skateboard < MainRecord

    def quality
      if skateboard.wheels = '4'
        return 'high_quality'
      else
        return 'low_quality'
      end
    end

  end
end

Main concerns:

  • Analysis ease
  • Query efficiency (grabbing People with 'quality' transporters in the backend without too much of a hassle)

I could use polymorphism, but given that these tables will mostly be used for analysis, the intermediary polymorphic table could be an annoying extra step. I also thought of having one transporter table with multiple columns but I don't think that's the best way.

Is there a way to set up 'polymorphism' without the extra intermediary table? Or is there a nicer solution for this setup?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文