# AR foreign key problem

**URL:** <https://www.ruby-forum.com/t/ar-foreign-key-problem/61494>\
**Category:** Rails\
**Created:** [May 24, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494 "2006-05-24T15:49:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [May 24, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494/1 "2006-05-24T15:49:09Z")

</div>

Hello Railslist,

(I’m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and  
SQLite3)

I have two tables in the database: “locations” and “customers”:

## customers.sql

## id INT PRIMARY KEY name VARCHAR 255 address VARCHAR 255 postal\_code VARCHAR 255 city VARCHAR 255

## locations.sql

## id INT PRIMARY KEY postal\_code TEXT longitude FLOAT latitude FLOAT

I’ve created two models (customer.rb and location.rb):

## customer.rb

## class Customer \< ActiveRecord::Base belongs\_to :location, :foreign\_key=\>‘postal\_code’ validates\_presence\_of :name, :postal\_code end

## location.rb

## class Location \< ActiveRecord::Base has\_many :customers end

The locations table is filled with \>300,000 records.  
The customers table has a couple of records with postal\_codes that  
match the locations table.

## In the controller, I have…

## def test @customer = Customer.find(1) end

## In the view, I have…

## \<%= @customer.name %\>

…which yields the first name-record in the customers table, so that’s  
OK.

## But shouldn’t I be able to now do something like:

## \<%= @customer.location.latitude %\>

…and expect it to return the corresponding latitude from the locations  
table?

## I get the nil error:

## “You have a nil object when you didn’t expect it! The error occured while evaluating nil.latitude”

What could be wrong?  
Is it something with the way I’ve set up the alternative foreign-key?  
or is it a plural/singular naming problem?

Thanks for any help.

Gijs N.

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [May 24, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494/2 "2006-05-24T16:11:09Z")

</div>

Replace the postal\_code field in customers with a postal\_code\_id int  
field, then you should be able to get that working.  
-N

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [May 24, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494/3 "2006-05-24T16:54:39Z")

</div>

does the table postal\_code\_in need to be of type INT per se?  
I still get exactly the same error (nil object) as before (I restarted  
webrick)

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [May 25, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494/4 "2006-05-25T15:01:05Z")

</div>

Oh yeah, that’s what I meant. I was thinking you had a postal\_codes  
table, because I’ve got one of those. But using an int location\_id  
would do the job.  
-N

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [May 25, 2006, 8:27am UTC](https://www.ruby-forum.com/t/ar-foreign-key-problem/61494/5 "2006-05-25T08:27:38Z")

</div>

1. postal\_code column in both the tables should be of same type  
if that is not working then having a location\_id column in customers  
table,  
referring to id column of locations table will surely work; with this  
you  
can skip :foreign\_key option, Rails will take care of that.

Regards,  
Jatinder
