建模多态博客文章
假设我有一个 blog_posts 表。但是,发帖的用户属于群组,每个群组都有自己的博客。
例如:
假设某个用户属于 3 个组:营销、Project Alpha、管理员
他创建了一篇博客文章,但希望该帖子出现在“营销博客”和“Project Alpha”博客中。
对此进行建模的最佳方法是什么?
是一个坏主意吗?
在 blog_posts 表中有一个字段(例如:group_ids)并存储以逗号分隔的 ids 列表:3,7(其中 Marketing Group =3,Project Alpha=7)
或者我应该创建另一个表并存储 blog_posts id 和组?
谢谢。
Say I have a blog_posts table. But, the users writing posts belong to groups, and each group has its own blog.
For example:
Say, a user belongs to 3 groups: Marketing, Project Alpha, Administrators
He creates a blog post, but want that post to appear in the "Marketing Blog" and on the "Project Alpha" blog.
What would be the best way to model this?
Would it be a bad idea to have a field in the blog_posts table like: group_ids
and store a comma-delimited list of ids: 3,7 (where Marketing Group =3, and Project Alpha=7)
or should I create another table and store blog_posts id and groups?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于@minitech的答案,我有3个模型和5个db表
模型
rails g model
其他表
rails g migration
然后在您的模型中配置关系,如下所示
然后根据您的示例,代码变为:
Building on @minitech's answer i'd have 3 model's and 5 db tables
Models
rails g model <model_name>
Additional tables
rails g migration <table_name>
Then in your models configure the relations as follows
Then as per your example the code becomes:
我建议创建一个表来保存所有博客的所有帖子,然后创建一个表来存储所有博客的信息,然后创建一个表来存储所有博客的帖子(通过 ID 引用)。然后你只需添加,例如:
到帖子中:ID =(自动增量值),Title ='标题',Content ='内容'
进入博文:blogID=(插入博文的博客#1的ID),postID=(上次自动递增的值)
进入博文:blogID=(要插入博文的博文#2的ID), postID=(上次的自增值)
这样,博文就被链接进来了,这样当编辑或删除的更改会反映在所有博客上,您还可以使用
SELECT * FROM blogPosts WHERE postID=id_of_post
等方式获取帖子所在的所有博客的快速列表。I would recommend creating one table to hold all posts for all blogs, then have a table to store all the blogs' information, then a table to store all the blogs' posts, referenced by ID. Then you just add, for example:
Into posts: ID=(auto increment value), Title='The title', Content='The content'
Into blog posts: blogID=(the ID of blog #1 to insert the post into), postID=(the auto increment value from last time)
Into blog posts: blogID=(the ID of blog #2 to insert the post into), postID=(the auto increment value from last time)
That way, the post is linked in so when it is edited or removed changes are reflected on all blogs, and you can also get a quick list of all blogs a post is in using something like
SELECT * FROM blogPosts WHERE postID=id_of_post
.使用逗号分隔列表作为使所有数据项原子化的方法是一个坏主意。
它基本上是一种获取不符合第一范式的数据并进行转换的方法,使其看起来像第一范式数据,即使它不是。
如果这样做,对于某些查询,您将必须对其中一个表进行完整扫描,而不是索引查找。这将非常缓慢,并且随着数据量的增加而变得更糟。
更好的解决方案是使用一个将 group_id 与 user_id 相关联的联结表。
然后,当您希望将所有数据放在一起时,进行三向联接。这是建模多对多关系的基本方法。
Using comma delimited lists as a way around making all data items atomic is a bad idea.
It's basically a way of taking data that is not in first normal form and transfomring so that it looks like first normal form data even though it's not.
If you do this, for certain queries you will have to do a full scan of one of the tables instead of an indexed lookup. This will be monstrously slow, and get worse as data volume increases.
A better solution is to have a junction table that relates group_id to user_id.
Then do a three way join when you want all the data together. This is the basic way you model a many-to-many relationship.