Ruby on Rails 中的多表继承与单表继承 [英] Multiple Table Inheritance vs. Single Table Inheritance in Ruby on Rails

查看:50
本文介绍了Ruby on Rails 中的多表继承与单表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时我一直在思考我应该走哪条路线.我有一个通知模型.到目前为止,我一直使用一个 notification_type 列来管理类型,但我认为最好为通知类型创建单独的类,因为它们的行为不同.

I have been struggling for the past few hours thinking about which route I should go. I have a Notification model. Up until now I have used a notification_type column to manage the types but I think it will be better to create separate classes for the types of notifications as they behave differently.

目前,可以通过 3 种方式发送通知:短信、推特、电子邮件

Right now, there are 3 ways notifications can get sent out: SMS, Twitter, Email

每个通知将具有:

id
subject
message
valediction
sent_people_count
deliver_by
geotarget
event_id
list_id
processed_at
deleted_at
created_at
updated_at

似乎 STI 是一个不错的候选人,对吗?当然,Twitter/SMS 不会有主题,Twitter 也不会有 sent_people_count、告别词.在这种情况下,我会说他们共享大部分领域.但是,如果我为 twitter 添加一个reply_to"字段并为 DM 添加一个布尔值呢?

Seems like STI is a good candidate right? Of course Twitter/SMS won't have a subject and Twitter won't have a sent_people_count, valediction. I would say in this case they share most of their fields. However what if I add a "reply_to" field for twitter and a boolean for DM?

我的观点是,现在 STI 是有道理的,但在这种情况下,我可能会因为不只是从 MTI 开始而在未来踢自己吗?

My point here is that right now STI makes sense but is this a case where I may be kicking myself in the future for not just starting with MTI?

更复杂的是,我想要一个 Newsletter 模型,它是一种通知,但不同之处在于它不会使用 event_id 或 delivery_by.

To further complicate things, I want a Newsletter model which is sort of a notification but the difference is that it won't use event_id or deliver_by.

我可以使用大约 2/3 的通知基类字段查看通知的所有子类.STI 是一个明智的选择,还是我应该使用 MTI?

I could see all subclasses of notification using about 2/3 of the notification base class fields. Is STI a no-brainer, or should I use MTI?

推荐答案

鉴于有限的信息,我会说坚持使用 STI.

Given the limited info, I'd say stick with STI.

关键问题是:您的应用中是否存在您希望将所有类型的通知放在一起考虑的地方?如果是这样,那么这是您想要坚持使用 STI 的强烈信号.

The key question is: Are there places in your app where you want to consider all types of Notifications together? If so, then that's a strong sign that you want to stick with STI.

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

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