On 6.10.2005, at 21.52, Belorion wrote:
> First of all, what exactly is the difference
> between :association_foreign_key and :foreign_key in a HABTM?
Straight from thee trenches (emphasis mine) [1]:
# :foreign_key - specify the foreign key used for the association. By
default this is guessed to be the ***name of this class*** in lower-
case and "_id" suffixed. So a Person class that makes a
has_and_belongs_to_many association will use "person_id" as the
default foreign_key.
# :association_foreign_key - specify the association foreign key used
for the association. By default this is guessed to be the ***name of
the associated class*** in lower-case and "_id" suffixed. So the
associated class is Project that makes a has_and_belongs_to_many
association will use "project_id" as the default association
foreign_key.
>
> Secondly, I am having problems getting HABTM working on a legacy
> db. I''ve tried using both association_foreign_key and foreign_key
> to describe the key name that is in the join table, but it doesn''t
> work. Rails continues to look for the "conventional" id (ie
> foo_id) instead of my own (fooId). Any help would be greatly
> appreciated.
You need to set both :f_k and :a_f_k in both classes according to the
instructions above because both of them are not obeying the convention.
//jarkko
>
> Below is a contrived example of what is *not* working for me (it
> keeps looking for the conventional key name instead of my specified
> key name). I''ve tried it with both association_foreign_key and
> foreign_key.
>
> Student < ActiveRecord::Base
> set_table_name :student
> set_primary_key :studentId
> has_and_belongs_to_many :teachers, :join_table =>
> ''student2teacher'', :association_foreign_key =>
''studentId''
> end
>
> Teacher < ActiveRecord::Base
> set_table_name :teacher
> set_primary_key :teacherId
> has_and_belongs_to_many :students, :join_table =>
> ''student2teacher'', :association_foreign_key =>
''teacherId''
> end
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails