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! -- Posted via http://www.ruby-forum.com/.
Peter Michaux
2006-Mar-13 05:08 UTC
[Rails] HABTM: two habtm''s between the same two tables
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 3/12/06, Gaudi Mi <gaudimila@yahoo.com> wrote:> 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Brian V. Hughes
2006-Mar-13 05:50 UTC
[Rails] HABTM: two habtm''s between the same two tables
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 > ... > endYou 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
David Corbin
2006-Mar-15 00:52 UTC
[Rails] HABTM: two habtm''s between the same two tables
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. On Sunday 12 March 2006 11:00 pm, Gaudi Mi wrote:> 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!