Rails - 具有多个值类型的EAV模型? [英] Rails - EAV model with multiple value types?

查看:165
本文介绍了Rails - 具有多个值类型的EAV模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个模型 Feature ,被其他各种模型使用;在这个例子中,它将被客户使用。

I currently have a model Feature that is used by various other models; in this example, it will be used by customer.

为了保持灵活性,Feature用于存储诸如名字,姓氏,名字, ,公司注册号等。

To keep things flexible, Feature is used to store things such as First Name, Last Name, Name, Date of Birth, Company Registration Number, etc.

您会注意到这个问题 - 虽然大多数都是字符串,出生日期的功能最好存储在列类型为Date(并且将是日期选择器,而不是视图中的文本输入)。

You will have noticed a problem with this - while most of these are strings, features such as Date of Birth would ideally be stored in a column of type Date (and would be a datepicker rather than a text input in the view).

如何最好地处理?目前我只有一个字符串列value;我已经考虑使用多个值列(例如string_value,date_value),但是这似乎不是特别有效,因为在每个记录中总是有一个空列。

How would this best be handled? At the present time I simply have a string column "value"; I have considered using multiple value columns (e.g. string_value, date_value) but this doesn't seem particularly efficient as there will always be a null column in every record.

任何关于如何处理这个问题的建议 - 感谢!

Would appreciate any advice on how to handle this - thanks!

推荐答案

有几种方法我可以看到你用这个,对你的需求。我不完全满意任何这些,但也许他们可以指向正确的方向:

There are a couple of ways I could see you going with this, depending on your needs. I'm not completely satisfied with any of these, but perhaps they can point you in the right direction:


  1. 序列化所有

Rails可以将任何对象存储为字节流,在Ruby中,一切都是一个对象。所以在理论上,你可以存储任何对象的字符串表示,包括Strings,DateTimes,甚至你自己的模型在数据库列。 Marshal 模块在大多数情况下处理此问题,并允许您撰写自己的

Rails can store any object as a byte stream, and in Ruby everything is an object. So in theory you could store string representations of any object, including Strings, DateTimes, or even your own models in a database column. The Marshal module handles this for you most of the time, and allows you to write your own serialization methods if your objects have special needs.

优点:在单个数据库列中真正存储任何内容。

Pros: Really store anything in a single database column.

缺点:在数据库中处理数据的能力是最小的 - 基本上不可能使用这个列作为存储之外的任何东西 - 你(可能)不能基于它排序或过滤你的数据,

Cons: Ability to work with data in the database is minimal - It's basically impossible to use this column as anything other than storage - you (probably) wouldn't be able to sort or filter your data based on it, since the format won't be anything the database will recognize.


  1. 每种数据类型的列

这是基本上你在问题中建议的解决方案 - 确定你可能需要存储哪些数据类型 - 你提到字符串和日期戳。如果没有太多的那些,可以简单地具有每个类型的列,并且只存储数据在其中之一。您可以覆盖属性访问器函数以使用正确的列,并且从外部,Feature将充当 .value ,无论您需要什么。

This is basically the solution you suggested in the question - figure out exactly which datatypes you might need to store - you mention strings and datestamps. If there aren't too many of those, it's feasible to simply have a column of each type and only store data in one of them. You can override the attribute accessor functions to use the proper column, and from the outside, Feature will act as though .value is whatever you need it to be.

优点:只需要一个表。

缺点:每个记录至少有一个空值。

Cons: At least one null value in every record.


  1. 多个模型/表

您可能需要的各种功能 - TextFeature,DateFeature等。这个多表继承的指南传达了想法和方法。

You could make a model for each of the sorts of Feature you might need - TextFeature, DateFeature, etc. This guide on Multiple Table Inheritance conveys the idea and methodology.

优点:没有空值 - 每个记录只包含它需要的列。

Pros: No null values - every record contains only the columns it needs.

缺点:复杂性。除了需要多个模型,如果您需要直接在数据库中使用不同类型的功能,您可能会发现自己正在进行复杂的连接和联合。

Cons: Complexity. In addition to needing multiple models, you may find yourself doing complex joins and unions if you need to work directly with features of different kinds in the database.

这篇关于Rails - 具有多个值类型的EAV模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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