Hello all. 
I have a student model with two foreign keys (FK). Both of these keys
point at a county table.
These are the column statements for the two FK:
t.column :nationality_id, :integer, :null => FALSE
t.column :current_location_id, :integer, :null => FALSE
I then linked these to FK into my Student and Countries models:
class StStudent < ActiveRecord::Base
belongs_to :nationality_id, :class_name => “GlCountry”, :foreign_key
=> “nationality_id”
belongs_to :current_location_id, :class_name => “GlCountry”,
:foreign_key => “current_location_id”
class GlCountry < ActiveRecord::Base
has_many :st_students, :class_name => “StStudent”, :foreign_key =>
“nationality_id”
has_many :st_students, :class_name => “StStudent”, :foreign_key =>
“current_location_id”
OK, so now things look peachy. I can do a
StStudent.nationality_id.printable_name and see happy things.
However, when I attempt to use this setup in the ‘New Student’ and ‘Edit
Student’ actions the troubles start!
My controller defines the countries like this:
@countries = GlCountry.find(:all, :select => “id, printable_name”,
:order => ‘printable_name’).map {|u| [u.printable_name,u.id]}
I then make dropdowns like this in the ‘_forms’ partial:
Nationality (*)
<%= select('st_student', 'nationality_id', @countries)%>
And now everything breaks. In the ‘New’ view I see the object ID for a
GlCountry object in the dropdown. If I try to save a new student or
edit an existing one I see a ‘GlCountry expected, got String’ error.
The dropdowns won’t select existing values when editing a student. I
can do it manually, but I have to type this: :selected =>
st_student.nationality_id.id.
I believe that the linking of the two models via the ‘:class_name =>’
statements in the models has created GlCountry objects that are
assciated w/ my StStudent object. It doesn’t seem to just return the
values as I would have wished.
Does anyone have any advice or solutions on this? Has anyone else
linked FK using the ‘:class_name =>’ method, and if so, how did you do
it?
Many thanks!
Nathan