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
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?