# Two foreign keys in model pointing at same table issue

**URL:** <https://www.ruby-forum.com/t/two-foreign-keys-in-model-pointing-at-same-table-issue/118077>\
**Category:** Rails\
**Created:** [October 23, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/two-foreign-keys-in-model-pointing-at-same-table-issue/118077 "2007-10-23T18:44:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e95f7d/32.png) [@nate](https://www.ruby-forum.com/u/nate)\
**Post date:** [October 23, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/two-foreign-keys-in-model-pointing-at-same-table-issue/118077/1 "2007-10-23T18:44:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e95f7d/32.png) [@nate](https://www.ruby-forum.com/u/nate)\
**Post date:** [October 23, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/two-foreign-keys-in-model-pointing-at-same-table-issue/118077/2 "2007-10-23T21:06:20Z")

</div>

On 23 Oct 2007, at 17:44, Nathan Rasch wrote:

> 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
