Rails - how would you set up this HABTM relationship
I have a situation where Project can have many Tags (and vice versa), so I have set up a has_and_belongs_to_many 开发者_如何学Gorelationship between the two.
My question is this: I need a project to have the ability to carry a single current tag.
Option 1
Can I add a tag_id to my Project table? How do I set up the relationship correctly, given there is already a `projects_tags' model?
Option 2
I'd imagine it isn't effective to have the projects_tags model carry a boolean current field, because it would require additional queries in practice for me to find the right relationship. I just throw it out there as an alternative I've considered.
You could use something like this:
class Project < ActiveRecord::Base
belongs_to :current_tag, :class_name => Tag
end
And then have current_tag_id in your projects table.
You can't add current to projects_tags because it's not technically a model: it's only a join table. You'd have to incorporate another model and use has_many :through to do it that way.
加载中,请稍侯......
精彩评论