在 Rails 应用程序中实现类似 Gmail 的标签
这更多的是一个应用程序设计问题,而不是一个特定的 Rails 问题。我正在尝试在 Gmail 中实现类似“标签”的系统。所以我希望用户能够将一个(或多个)标签应用于特定的帖子。但是,我还希望用户能够使用标准 Rails CRUD 管理标签。
假设我有一个名为 Post 的模型。所以一个用户有很多帖子。用户可以创建与帖子不同的模型标签,但也可以使用帖子新建/编辑视图中的复选框将这些标签应用于帖子。如果用户删除特定标签,它只会从帖子视图中删除,不会以任何其他方式影响帖子。
这种情况表明我缺乏对数据库关系的了解,但这是否是 has_many_and_belongs_to_many 关系的情况?
如果是这样,下面的设计是否可以实现这一点?
- 用户 has_many 帖子
- 用户 has_many 标签
- 帖子 has_many_and_belongs_to_many 标签
- 标签 has_many_and_belongs_to_many 帖子
也许我想太多了,我的逻辑已经偏离了。
This is more of an application design question then a specific Rails question. I'm trying to implement a system like "labels" within Gmail. So I want a user to be able to apply a label (or many) to a specific post. However, I also want the user to be able to manage labels using standard Rails CRUD.
Let's say I have a model called Post. So a user has many Posts. A user can create labels which is a separate model from Posts, but these can also be applied to posts with the use of checkboxes in the Post new/edit views. If a user deletes a specific label, it simply gets removed from the Post view and doesn't affect the Post in any other way.
This situation shows my lack of knowledge of db relationships, but is this a case for a has_many_and_belongs_to_many relationship?
If so, would the following design be the way to implement this?
- User has_many Posts
- User has_many Labels
- Post has_many_and_belongs_to_many
Labels - Label has_many_and_belongs_to_many
Posts
Maybe I'm over thinking this and my logic is way off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须决定是否希望标签由人们“拥有”或只是由人们“使用”。在 Gmail 中,我的标签非常适合我。在 SO 中,尽管我可以创建标签,但它们并不是特定于我的。
如果您想要标签而不是标签,那么是的。
如果需要,您可以用英语定义它们。
我不会说您已经过度思考了这个概念数据模型。
You have to decide whether or not you want tags to be "owned" by people or just "used" by people. In gmail, my labels are very specific to me. In SO the tags are not specific to me even though I can create them.
If you want labels as opposed to tags then yes.
You can define these in English if you want.
I wouldn't say you've over-thought this CONCEPTUAL data model.
从你的问题来看,你似乎正在尝试建立一个分类系统(即标记模型)。您可能需要查看 Acts As Taggable On 作为可能的解决方案(或者只是一个有趣的建模参考)来解决您的问题。
From your question, it seems like you are trying to build a taxonomy system (i.e. tagging model). You may want to check out Acts As Taggable On as a possible solution (or just an interesting modeling reference) for your problem.
听起来您可以通过标签来实现此功能。查看这篇文章,获取 Rails 标记插件和 gem 的列表。
It sounds like you could implement this functionality through tags. Check out this post for a list of tagging plugins and gems for rails.