Imagine I want to track people, and the clubs that they belong to. table people with columns person_id, person_name table clubs with columns club_id, club_name And I have the association table: table clubs_people with columns person_id, club_id Now I know how to do this habtm between the two, in order to associate people with clubs that they belong to. However my application also needs a secondary association just like this: imagine I want to track the clubs that a person has APPLIED to join, but is not yet a member. So I have a second association table table clubs_people_applied with columns person_id, club_id In other words, I need TWO habtm's between the same two tables. Where this is failing I think is in the model definitions: Class Person < ActiveRecord::Base has_and_belongs_to_many :clubs, :join_table => clubs_people has_and_belongs_to_many :clubs, :join_table => clubs_people_applied ... end The rails api says that "WARNING: If youâ??re overwriting the table name of either class, the table_name method MUST be declared underneath any has_and_belongs_to_many declaration in order to work." Is this the problem? I don't understand what this means exactly, that is I don't know how to declare the method. Thanks!
on 2006-03-13 05:00
on 2006-03-13 06:08
I'm not sure if either of these are the problems but in the people table why not have the primary key be just "id" instead of "person_id"? Same for the clubs table. For the habtm statement, when you use :join_table I think the table name has to be in quotation marks to make it a string :join_table => "clubs_people" In some cases, you might have to specify the foreign key in the join table. Maybe one of these will fix the problem. I've used habtm specifying the join table without problems. It should work. I'm looking forward to life without habtm once Rails 1.1 comes out with the has_many :through business. (But thanks to the habtm author for what habtm enabled.) - Peter
on 2006-03-13 06:51
Gaudi Mi wrote: > Where this is failing I think is in the model definitions: > > Class Person < ActiveRecord::Base > has_and_belongs_to_many :clubs, :join_table => clubs_people > has_and_belongs_to_many :clubs, :join_table => clubs_people_applied > ... > end You can't use the same symbol (:clubs) as the object in more than one association. That's what the Warning is trying to tell you. You probably want the second habtm to read something like: has_and_belongs_to_many :applied_clubs, :join_table => clubs_people_applied -Brian
on 2006-03-15 01:53
Be aware that rails 1.0 has bug such that two habtm's in one class just doesn't work right. It's been fixed in Edge. I can dredge up the exact bug/fix in svn if you're interested.