使用关系数据库中简单数据的多对多关系的替代方法

发布于 2024-12-02 00:17:42 字数 465 浏览 2 评论 0原文

我正在开发一个 ruby​​ on Rails 项目,该项目需要记录一些适合多对多关系的简单用户数据(将其视为一组用户兴趣)。我有使用 has_many :through 和 HABTM 处理多对多关系的经验,但是当事情足够简单以至于可以表示为单个单词时,创建新模型并必须执行所需的连接似乎有点矫枉过正来访问它。我认为除了兴趣本身的名称之外,没有太多必要记录其他属性,因此使用模型的论点没有说服力。我知道 noSQL 对此很有用,但我想知道从性能和良好设计的角度来看,使用关系数据库执行此操作的正确方法是什么(我正在使用 postgres)。我正在考虑使用的方法是使用文本数据类型将其记录为用户模型中的分隔字符串,然后将其解析为数组。或者这可以是表示哈希键数组的字符串,然后该数组会查询哈希表或键/值存储数据库以获取数据。我确信对此有很多不同的方法,但我很高兴知道最佳实践是什么,因为我预计将来我将不得不经常实施这一点。另外,如果您可以推荐任何有助于此目的的工具/宝石/框架,那将非常有帮助。

谢谢。

I'm working on a ruby on rails project that requires recording some simple user data which fits in a many-to-many relationship (think of it as an set of user interests). I'm experienced with doing many to many relations using has_many :through and HABTM, but when the thing is simple enough that it could be represented as a single word it seems like overkill to create a new model and to have to do the required joins to access it. I don't see much need in recording other properties than the name of the interest itself, so that argument for using a model isn't persuasive. I know noSQL can be good for this, but I'm wondering what the right way to do this with a relational db would be (I'm using postgres), both from a performance and good design perspective. The approach I'm thinking of using would be to record it as a delimited string in the user model using the text datatype and then parse this into an array. or this could be a string representing an array of hash keys which then queries a hash table or key/value store db for the data. I'm sure there many different approaches to this, but I'd appreciate knowing what the best practice would be, since I expect I'll be having to implement this in the future a lot. Also if you could recommend any any tools/gems/frameworks that assist in this that would be very helpful.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-12-09 00:17:42

基本上以某种方式序列化数据并将其存储在预先知道您不会对其进行查询的列中。我见过人们使用的一种相当紧凑的解决方案是将其存储为 json ,这样您就不必做任何复杂的事情了。您只需要一个足够大的 varchar 类型来存储 json 字符串。

Basically serialize the data in some way and store it in a column knowing in advance that you will not query against it. One fairly compact solution I've seen people use is to store it as json which relieves you of having to do anything complicated. You simply need a varchar type that is large enough to store the json strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文