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. On 6/22/06, Jason Frankovitz <jason@seethroo.us> wrote:> > 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/424a2330/attachment.html
> > Message: 4 > Date: Thu, 22 Jun 2006 14:10:16 -0500 > From: "Brian Hogan" <bphogan@gmail.com> > Subject: Re: [Rails] id column for join table... kosher? > To: rails@lists.rubyonrails.org > Message-ID: > <95a868100606221210s1d67d95aq3cbaedf497181a7a@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > 1. migration > create_table :bookmarks_tags, :id=>false do |t| > t.column :bookmark_id. :integer > t.column :tag_id, :integer > endCool, thanks.> > 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 >I do have add''l fields for the join table so has_many :through looks like the way to go. I know this is a new feature of Rails. Are there any good tutorials about implementing it?> 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!) >THanks, I saw that. What I''m doing right now won''t work with it.> 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?I think what I want to do is slightly unusual. I want to have a join table that has three ids being matched up, not just two. I want to identify a series of three-way relationships ("TWR''s") and attach meta-data to an individual TWR. For example, I want to combine users, bookmarks, and tags in one join table, then have additional info that records when a TWR record was created, what browser they used to create the TWR, etc. I guess you could think of it as an "Event" model involving the three id''s and meta-data. Can has_many :through handle a three-way? (err, ahem) Thanks in advance! -Jason