Implement Gmail-like labels in a Rails app
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 relati开发者_如何学编程onships, 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.
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.
User ---< Label
Label >--< Post
User ---< Post
You can define these in English if you want.
A user has zero, one or more Labels
A Label is used by one and only one user
A user has zero, one or more posts
A post is created by one and only one user
A post has zero, one or more labels
a label is applied to zero, one or more posts
I wouldn't say you've over-thought this CONCEPTUAL data model.
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.
It sounds like you could implement this functionality through tags. Check out this post for a list of tagging plugins and gems for rails.
精彩评论