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