views:

67

answers:

2

I am trying to model a publications. A publication can have multiple authors and editors. Since it is possible that one person is an author of one publication and an editor of another, no separate models for Authors and Editors:

class Publication < ActiveRecord::Base
  has_and_belongs_to_many :authors, :class_name=>'Person'
  has_and_belongs_to_many :editors, :class_name=>'Person'
end

The above code doesn't work, because it uses the same join table. Now I now that I can specify the name of the join table, but there is a warning in the API documentation is a warning about that which I don't understand:

:join_table: Specify the name of the join table if the default based on lexical order isn’t what you want. 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.

+1  A: 

I'd generally consider this a case where you want to use has_many :through.

Jakub Hampl
http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
Ermin
+1  A: 

It means that if class Publication is linked to a table with no standard name, example "my_publications":

class Publication < ActiveRecord::Base
  set_table_name "my_publication"
end 

The set table name should be put behind the habtm declaration for this to work:

class Publication < ActiveRecord::Base
  has_and_belongs_to_many :authors, :class_name=>'Person'
  has_and_belongs_to_many :editors, :class_name=>'Person'
  set_table_name "my_publication"
end
tommasop