为什么Rails中的ActiveRecord不支持多表继承? [英] Why does ActiveRecord in Rails not support Multiple Table Inheritance?

查看:64
本文介绍了为什么Rails中的ActiveRecord不支持多表继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现我在纸上放在一起的一组模型,但遇到一个问题,我认为最好的方法是使用多表继承设置。但是,在Google搜索之后,我发现ActiveRecord不支持MTI ...即使有很多文章说明了如何实现。这让我想知道如果未实现我的模型是否正确设置。所以我的问题是为什么Active Record没有内置对MTI的支持?如果您对我的模型设置准备就绪感到好奇,我将其保留在下面。

I was trying to implement a set of models I had put together on paper and ran into an issue where I thought the best way to go about it would be using a Multiple Table Inheritance setup. However, after google searching I found that ActiveRecord doesn't support MTI... even though there are plenty of articles showing how it can be done. This had me wondering if I was setting up my models correctly if it's not implemented. So my question is why doesn't Active Record have built in support for MTI? If you are curious to what my model setup was 'going' to look like I'll leave it below.

class Player < ActiveRecord::Base; end
class CollegePlayer < Player; end
class ProPlayer < Player; end

其中玩家可以是CollegePlayer和ProPlayer之一或两者都可以。或者在另一个示例中...

Where a Player can be either or both of CollegePlayer and ProPlayer. Or in another example...

class Person < ActiveRecord::Base; end
class User < Person; end
class Player < Person; end
class Coach < Person; end

其中人可以是用户前任 玩家和/或教练

Where a "Person" could be a User, former Player, and/or Coach.

推荐答案

此问题的简短答案是因为ActiveRecord核心团队不希望包含此功能,并且社区的需求不足。参见 https://github.com/rails/rails/issues/5541

This short answer to your question is because the ActiveRecord core team does not want to include this feature coupled with not enough demand from the community. see https://github.com/rails/rails/issues/5541

如果您想获得明确的答案,则必须询问DHH(david.heinemeierhansson.com)或Aaron Tender Love Patterson(tenderlove@github.com),因为它们是领先者

If you want the definitive answer you would have to ask DHH (david.heinemeierhansson.com) or Aaron Tender Love Patterson (tenderlove@github.com) as they are the "leading" contributors on the active record project.

我的个人回答是因为活动记录已经足够复杂。为ActiveRecord支持其已经支持的功能而进行的元编程量已经很难维护和扩展。当前致力于AR的核心团队一直将重点放在进一步优化AR以使其更快地工作以及对其进行重构以清理组织上。鉴于已经有了获取多个表(例如继承)的替代方法,并且社区并不欢迎此功能,因此优先级较低。

My personal answer is because active record is already complex enough. The amount of meta programming that is done for ActiveRecord to support the features it already support is already a bit challenging to maintain and extend. The core team that is currently working on AR have been putting a great deal of focus into further optimizing AR to work faster and refactoring it to clean up the organization. Given that there are already alternatives to getting multiple table like inheritance and the community isn't clamoring for this feature its less of a priority.

尽管要评论关于数据库组织的正确性的其他问题,但我同意muistooshort,看来您可以通过角色完成所需的工作。当我刚接触C ++时,我尝试为不同的对象创建单独的模型,并感到非常需要遵循某种复杂的继承方案。您只需要在应用程序现在站立时问自己(不是在路上,因为这是推测),这是行为差异,这些类型之间的差异如此之大,足以证明这种差异性是合理的。

Though to comment on your other question about the correctness of you DB organization I agree with muistooshort it seems like you can accomplish what you want with roles. When I was first coming from C++ I tried to make separate models for different objects and felt a strong need to adhere to some sort of convoluted inheritance scheme too. You just need to ask yourself as you app stands now (not down the road because thats speculation at this point) is the difference in behavior so different between these types that it justifies this degree of differentiation.

编辑

我采访了活跃机器人工作团队的Thoughtbot的Sean Griffin记录,我问他为什么不支持多表继承,他的回答是:

I spoke with Sean Griffin of Thoughtbot who works on the Active Record and I asked him why multiple table inheritance was not support his response was:


多态关联填补了大多数使用的角色例

这篇关于为什么Rails中的ActiveRecord不支持多表继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆