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:
<p><label for="st_student_nationality_id">Nationality
(*)</label><br/>
<%= select(''st_student'',
''nationality_id'', @countries)%></p>
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Oct-23 19:05 UTC
Re: Two foreign keys in model pointing at same table issue
On 23 Oct 2007, at 17:44, Nathan Rasch wrote:> 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. >I think the problem is that the association name is the same as the foreign key name. When populating the select, normally you would look at foo_id to see which Foo should be the selected one. However in your case sending nationality_id to an instance of Student returns a GlCountry object, not an id. Similarly the form stuff will be trying to set nationality_id to be the id of the chosen object, but since nationality_id is actually the association that will break too. You should probably change it to this: class StStudent < ActiveRecord::Base belongs_to :nationality, :class_name => "GlCountry", :foreign_key => "nationality_id" belongs_to :current_location, :class_name => "GlCountry", :foreign_key => "current_location_id" end Fred