主动记录:我可以在没有实际多态性的情况下创建类似多态性的关联吗?
我不确定该如何说出这个问题(欢迎编辑),但这是我的问题:说我正在为现有结构添加到现有People
表格,高级表表现类似:
- 有很多
人
- a
人
可以有几种不同类型的转运者
,即。Transporter ::滑板
,Transporter :: Car
,等等。- 每个
Transporter
可能会有两个有用的列:- 外键
person_id
- 值列(将用于计算班级中的“质量”)(即
model
fortransporter :: Car
或wheels
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 ofTransporters
, 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
forTransporter::Car
orwheels
forTransported::Skateboard
)
- the foreign key
- There will likely only ever be 3-5 types of
Transporters
- Each
- 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 itsvalue
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论