建模多态博客文章

发布于 2024-11-08 11:51:54 字数 358 浏览 3 评论 0原文

假设我有一个 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 技术交流群。

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

发布评论

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

评论(3

︶ ̄淡然 2024-11-15 11:51:55

基于@minitech的答案,我有3个模型和5个db表

模型
rails g model

  • 帖子(必须有 FK 的 user_id 列)
  • 用户

其他表
rails g migration

  • groups_posts(仅列 group_id 和 post_id)
  • groups_users(仅列 group_id 和 user_id)

然后在您的模型中配置关系,如下所示

class Post < ActiveRecord::Base
  has_and_belongs_to_many :groups
  belongs_to :user
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :posts
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_and_belongs_to_many :posts
end

然后根据您的示例,代码变为:

user = User.find(<id of user>)
post = Post.create(:title => 'foo', :content => 'bar', :user => user)
post.groups << Group.find(3)
post.groups << Group.find(7)

Building on @minitech's answer i'd have 3 model's and 5 db tables

Models
rails g model <model_name>

  • Post (must have column user_id for FK)
  • User
  • Group

Additional tables
rails g migration <table_name>

  • groups_posts (columns group_id and post_id only)
  • groups_users (columns group_id and user_id only)

Then in your models configure the relations as follows

class Post < ActiveRecord::Base
  has_and_belongs_to_many :groups
  belongs_to :user
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :posts
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_and_belongs_to_many :posts
end

Then as per your example the code becomes:

user = User.find(<id of user>)
post = Post.create(:title => 'foo', :content => 'bar', :user => user)
post.groups << Group.find(3)
post.groups << Group.find(7)
别再吹冷风 2024-11-15 11:51:55

我建议创建一个表来保存所有博客的所有帖子,然后创建一个表来存储所有博客的信息,然后创建一个表来存储所有博客的帖子(通过 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.

红焚 2024-11-15 11:51:55

使用逗号分隔列表作为使所有数据项原子化的方法是一个坏主意。
它基本上是一种获取不符合第一范式的数据并进行转换的方法,使其看起来像第一范式数据,即使它不是。

如果这样做,对于某些查询,您将必须对其中一个表进行完整扫描,而不是索引查找。这将非常缓慢,并且随着数据量的增加而变得更糟。

更好的解决方案是使用一个将 group_id 与 user_id 相关联的联结表。

 Create Table Group_user 
(group_id,
 user_id primary key is (gorup_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.

 Create Table Group_user 
(group_id,
 user_id primary key is (gorup_id,  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.

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