# Model Associations

**URL:** <https://www.ruby-forum.com/t/model-associations/191929>\
**Category:** Rails\
**Created:** [July 20, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/model-associations/191929 "2010-07-20T21:27:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tornado](https://www.ruby-forum.com/u/tornado)\
**Post date:** [July 20, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/model-associations/191929/1 "2010-07-20T21:27:17Z")

</div>

Hi,

I have just started using rails and i am trying to use IP database to  
get user location.

Here are the database schemas…

[code]CREATE TABLE IF NOT EXISTS `ip_group_cities` (  
`ip_start` bigint(20) NOT NULL,  
`location` int(11) NOT NULL,  
UNIQUE KEY `index_ip_group_cities_on_ip_start` (`ip_start`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `iso3166_countries` (  
`code` varchar(2) NOT NULL,  
`name` varchar(64) NOT NULL,  
UNIQUE KEY `index_iso3166_countries_on_code` (`code`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `locations` (  
`id` int(11) NOT NULL AUTO\_INCREMENT,  
`country_code` varchar(2) NOT NULL,  
`region_code` varchar(2) NOT NULL,  
`city` varchar(64) NOT NULL,  
`zipcode` varchar(8) NOT NULL,  
`latitude` float NOT NULL,  
`longitude` float NOT NULL,  
`metrocode` varchar(3) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO\_INCREMENT=2 ;[/code]

and here is the sql `SELECT locations.city,locations.id FROM
ip_group_cities,locations where ip_group_cities.ip_start <=
INET_ATON('$ip') AND ip_group_cities.location = locations.id order by
ip_group_cities.ip_start desc limit 1`

i have used these associaltions

`class Iso3166Country < ActiveRecord::Base
  has_many :locations, :foreign_key: :id
end
`

`class IpGroupCity < ActiveRecord::Base
  has_one :locations, :foreign_key => :id
end
`

`class Location < ActiveRecord::Base
  has_many :ip_group_cities, :foreign_key => :location
  belongs_to :iso3166_countries, :foreign_key => :country_code
end
`

But when i run this code

```auto
C = Location.find(1);
```

It does not show me the columns from the other tables… I mean i dont  
see a name column from the Iso3166Countries table.

What am i doing wrong?

---

<div class="post-metadata">

**Author:** ![tornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tornado](https://www.ruby-forum.com/u/tornado)\
**Post date:** [July 20, 2010, 9:54pm UTC](https://www.ruby-forum.com/t/model-associations/191929/2 "2010-07-20T21:54:30Z")

</div>

> ```auto
> C = Location.find(1);
> ```
> 
> It does not show me the columns from the other tables… I mean i dont  
> see a name column from the Iso3166Countries table.

All you are asking rails to do is load a location object - what were you  
expecting it to do?

Fred

---

<div class="post-metadata">

**Author:** ![tornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tornado](https://www.ruby-forum.com/u/tornado)\
**Post date:** [July 20, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/model-associations/191929/3 "2010-07-20T22:00:56Z")

</div>

On 20 July 2010 20:27, Amit K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> UNIQUE KEY `index_ip_group_cities_on_ip_start` (`ip_start`)  
> `id` int(11) NOT NULL AUTO\_INCREMENT,  
> and here is the sql `SELECT locations.city,locations.id FROM
> ip_group_cities,locations where ip_group_cities.ip_start <=
> INET_ATON('$ip') AND ip_group_cities.location = locations.id order by
> ip_group_cities.ip_start desc limit 1`
> 
> i have used these associaltions
> 
> [code]class Iso3166Country \< ActiveRecord::Base  
> has\_many :locations, :foreign\_key: :id

What is the foreign\_key spec here supposed to be doing?

> end  
> [/code]
> 
> [code]class IpGroupCity \< ActiveRecord::Base  
> has\_one :locations, :foreign\_key =\> :id

If you really mean has\_one then it should be :location (singular). I  
suspect you mean belongs\_to however since you have location has\_many  
IpGroupCities below. Again what is the foreign\_key spec for?

> end  
> [/code]
> 
> [code]class Location \< ActiveRecord::Base  
> has\_many :ip\_group\_cities, :foreign\_key =\> :location

Again I do not understand the foreign key spec

> belongs\_to :iso3166\_countries, :foreign\_key =\> :country\_code  
> end  
> [/code]
> 
> But when i run this code
> 
> [code]  
> C = Location.find(1);

You would be better to use lower case C.

> [/code]
> 
> It does not show me the columns from the other tables… I mean i dont  
> see a name column from the Iso3166Countries table.

Try C.iso3166\_country. It will not fetch the association data until  
you ask.

Colin
