Id column for join table... kosher?

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:

  1. 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.

  2. 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?

Thanks for any/all advice!
-Jason

  1. migration
    create_table :bookmarks_tags, :id=>false do |t|
    t.column :bookmark_id. :integer
    t.column :tag_id, :integer
    end

  2. 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.
http://wiki.rubyonrails.com/rails/pages/Acts+As+Taggable+Plugin(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

@bookmark = Bookmark.find(1)
@tags = @bookmark.tags

@bookmark.tags << Tag.create :name => "ajax"


etc

So what would you be doing with joins?

Hope some of that helps you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs