Rails:模型上的动态列/属性? [英] Rails: dynamic columns/attributes on models?

查看:280
本文介绍了Rails:模型上的动态列/属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SaaS应用程序,其中帐户希望在用户模型上保存不同类型的信息。例如,一个帐户可能希望保存年龄和出生日期,但在另一个帐户中,他们不会使用这些列,并且希望保存关于头发颜色和高度的信息。

I have a SaaS application, where accounts want to save different types of information on a User model. So for example, one account may want to save age, and birthdate, but in another account, they won't be using those columns and will want to save info on hair color, and height.

这些只是例子,但是我如何构建我的模型和数据库,使其在自定义,动态列中工作得很好,而不会创建太多的空属性。 p>

These are just examples, but how would I structure my model and db so that it works well with "customized, dynamic" columns without creating too many empty attributes.

推荐答案

对我来说,这听起来像一个完美的例子,你想使用一个无模式的NoSQL数据库。

To me this sound like a perfect example were you want to use a schema free NoSQL database.

或者(如果你想坚持使用SQL)你可以有一个 User $ c> has_many:attributes 关联。其中属性只是('age','birthday','hair color')和值的组合。复杂性带有不同的数据类型和同时包含多个属性的查询。

Or (if you want to stick with SQL) you can have a base User model with a has_many :attributes association. In which a attribute is just a combination of a key ('age', 'birthday', 'hair color') and a value. Complexity comes with different datatypes and queries covering multiple attributes at the same time.

这篇关于Rails:模型上的动态列/属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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