Ruby on Rails的 - 替代性传播疾病? [英] Ruby on Rails - Alternatives to STI?

查看:157
本文介绍了Ruby on Rails的 - 替代性传播疾病?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多不同的模式(接近20),其具有一些共同的特征,但也有不同在一定程度上别人。 STI好像一开始有吸引力,但我不知道如何将各种车型将会随着时间的推移与快速的产品开发。

I have many different models (close to 20) that share some common attributes but also differ to some degree in others. STI seems attractive at first, but I have no idea how the various models will evolve over time with rapid product development.

一个平行于我们的应用程序很好想到的是Yelp的。如何将Yelp的管理的东西在Rails的?所有的贴子有像地址的一些共同属性。然而,它们之间的区别相当多的人。例如,你有餐馆预订选项,也许不是为别人。餐厅也有一吨的其他属性,如酒入内,并不适用于其他人。与STI这样做会走出手pretty的快。

A good parallel to our application that comes to mind is Yelp. How would Yelp manage something in Rails? All of the postings have some common attributes like "address". Yet, they differ quite a lot on others. For example, you have a reservation option for restaurants and maybe not for others. Restaurants also have a ton of other attributes like "Alcohol allowed" that don't apply to others. Doing this with STI will get out of hand pretty quickly.

所以,什么下一个最好的选择吗? HStore Postgres的?我不喜欢使用HStore任何东西,但小事情。 HStore解决了一些问题,同时介绍其他像缺乏数据类型,缺乏参照完整性检查等。我想了坚实的关系型数据库,以奠定一个基础。所以在Yelp的情况下,很可能,一间餐厅型号是我要去的地方。我已经采取了看看喜欢这里的建议 - http://mediumexposure.com/multiple - 表继承主动记录/ 的,但我不开心了就做这么多猴子修补得到的东西很常见下去。

So whats the next best option? HStore with Postgres? I am not comfortable using HStore for anything but small things. HStore solves some problems while introduces others like lack of data types, lack of referential integrity checks etc. I'd like a solid relational database as the foundation to build upon. So in the Yelp case, probably, a restaurant model is where I am going. I've taken a look at suggestions like here - http://mediumexposure.com/multiple-table-inheritance-active-record/, but I am not happy to do so much monkey patching to get something so common going.

所以,我想知道存在哪些其他的替代品(如果有的话),或者我应该只是咬紧牙关,磨我的牙齿和复制这些公共属性到20款?我想我的问题将来自迁移文件,而不是C本身的$ C $。举例来说,如果我通过设置表我的迁移循环,并设置这些属性在桌子上,然后我会已经缓解了问题的严重程度具有不同的模式?

So I am wondering what other alternatives exist (if any) or should I just bite the bullet, grind my teeth and copy those common attributes into the 20 models? I am thinking my problems would come from the migration files rather than the code itself. For example, if I setup my migrations to loop through tables and set those attributes on the tables, then would I have mitigated the extent of the problem with having different models?

我俯瞰,可能会导致大量的问题,在道路上有独立的车型至关重要的东西?

Am I overlooking something critical that might cause a ton of problems down the road with a separate models?

推荐答案

我看到几个选择这里:

  1. 硬着头皮有很多相同的属性创建20个不同的型号。这可能是因为这些模型会随时间漂移 - 增加新的领域,以一种特定类型 - 你将创建一个200列的表与性病。也许你不 - 未来是很难看到,尤其是探索性/敏捷软件

  1. Bite the bullet and create your 20 different models with a lot of the same attributes. It's possible that these models will drift over time - adding new fields to one specific type - and you'll create a 200 column table with STI. Maybe you don't - the future is hard to see, especially with exploratory/agile software.

在一个NoSQL的(文件),数据库存储非指涉领域。用你的零件的记录是关系(一个用户有很多评论和评论有一个业务)关系型数据库,但保留类型的具体的东西,在一个NoSQL数据库。保持一个 external_document_id 在Rails的模型和 external_record_id / external_record_type 在你的NoSQL文档的架构,所以你仍然可以查询所有的酒吧,允许吸烟使用任何NoSQL的ORM你最终使用。

Store non referential fields in a NoSQL (document) database. Use your relational database for parts of the record that are relational (a user has many reviews and a review has one business), but keep the type specific stuff in a NoSQL database. Keep an external_document_id in your Rails models and external_record_id / external_record_type in your NoSQL document schema so you can still query all bars that allow smoking using whatever NoSQL ORM you end up using.

创建一个属性模式。一个属性 belongs_to的:parent_object,多态:真正的字段。通过这种方法,你可能有一个基商业模式,每个企业可以的has_many:属性。某些(非关系?)业务( allows_smoking )是一个属性记录的属性。一个属性的重点可能是一个字符串,也可以是一个数字,你有Ruby的常量。你基本上使用属性实体创建选项#2版本的SQL。这可能是一个不错的选择,而且我已经使用这个自己的用户或个人资料的模型。 (虽然也有一些性能影响意识到这种方法)。

Create an Attributes model. An attribute belongs_to :parent_object, polymorphic: true with a key and value field. With this approach you might have a base Business model and each business can has_many :attributes. Certain (non-relational?) attributes of the business (allows_smoking) are one Attribute record. An Attribute's key could be a string or could be a numeral you have Ruby constants for. You're essentially using the Attribute entities to create a SQL version of option #2. It might be a good option, and I've used this myself for User or Profile models. (Although there are some performance hits to be aware of with this approach).

我真的很担心有许多(独立)模式的东西,听起来子类,安永。这是可能的,你也许可以干起来共同行为/使用的关注(语法糖在混入​​概念的方法,看到一个真棒SO回答在Rails的关注4 )。你还有你的(最初的)迁移问题,当然。

I'd really worry about having that many (independent) models for something that sounds subclass-ey. It's possible you might be able to DRY up common behavior/methods by using Concerns (syntactic sugar over the mixin concept, see an awesome SO answer on concerns in Rails 4). You still have your (initial) migration problem, of course.

这篇关于Ruby on Rails的 - 替代性传播疾病?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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