I’ve got two tables, bookmarks & tags. Using a
has_and_belongs_to_many association, I can do lookups using a join
table called bookmarks_tags. Two questions:
Can I have a migration for my join tables? Rails seems to “know”
about join tables implicitly from the associations, but if I do a
rake migrate the join tables won’t be built. I’ve been creating them
using SQL/by hand.
Is there anything wrong with having an ‘id’ column on my join
table? Rails expects:
bookmark_id & tag_id
…but what if I want to reference individual joins? Can I do:
bookmark_id & tag_id & id
Does this mess anything up? The Agile book says don’t do it, articles
on the web say it’s OK. Who to believe?
migration
create_table :bookmarks_tags, :id=>false do |t|
t.column :bookmark_id. :integer
t.column :tag_id, :integer
end
No ids on join tables. However, see has_many :through That lets
you
do a has_and_belongs_to_many using a model as the middle piece instead
of a
join table. Then you can have an id on that table. However, I would
advise
against this unless you have additional fields on the join
2.b Looks like you’re doing tagging. Have a look ath the
acts_as_taggable
plugin. Peak Obsession(You
want the plugin, not the Gem!)
2 c. I don’t get your question about individual joins. With a HABTM
relationship, you just do