On 20 July 2010 20:27, Amit Kumar <[email protected]> wrote:
> 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

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to