# Model Associations

**URL:** <https://discuss.rubyonrails.org/t/model-associations/53092>\
**Category:** rubyonrails-talk\
**Created:** [July 20, 2010, 7:27pm UTC](https://discuss.rubyonrails.org/t/model-associations/53092 "2010-07-20T19:27:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [July 20, 2010, 7:27pm UTC](https://discuss.rubyonrails.org/t/model-associations/53092/1 "2010-07-20T19: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` ( &nbsp;&nbsp;`ip_start` bigint(20) NOT NULL, &nbsp;&nbsp;`location` int(11) NOT NULL, &nbsp;&nbsp;UNIQUE KEY `index_ip_group_cities_on_ip_start` (`ip_start`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

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

and here is the sql [code]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[/code]

i have used these associaltions

[code]class Iso3166Country \< ActiveRecord::Base &nbsp;&nbsp;has\_many :locations, :foreign\_key: :id end [/code]

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

[code]class Location \< ActiveRecord::Base &nbsp;&nbsp;has\_many :ip\_group\_cities, :foreign\_key =\> :location &nbsp;&nbsp;belongs\_to :iso3166\_countries, :foreign\_key =\> :country\_code end [/code]

But when i run this code

[code] C = Location.find(1); [/code]

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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 20, 2010, 7:53pm UTC](https://discuss.rubyonrails.org/t/model-associations/53092/2 "2010-07-20T19:53:56Z")

</div>

> ```ruby
> 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:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 20, 2010, 7:58pm UTC](https://discuss.rubyonrails.org/t/model-associations/53092/3 "2010-07-20T19:58:45Z")

</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 [code]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[/code]
> 
> 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
