开发者

Rails City Listings

I'm relatively new to rails and I'm working on a task which requires a similar functionality to the Cities tab here: http://开发者_JAVA技巧haystack.com/cities . I've searched all the books and can't seem to understand the logic that makes all of this possible. The only thing I can think of is reverse engineer e-commerce code to do what I want. Does anyone have any bright ideas?


You're going to at least need to get a listing of city/state data, either from somewhere like geonames.org or zipcodeworld.com. You'll then have to map cities to states, states to countries, and then figure out a clean way to display that, much like the haystack.com site. I would guess top cities is either weighted by how many users have requested each city or weighted simply by supposed popular cities.

Mostly it will just involve how you relate each of those data types (City, State, Country) together. Actually displaying, apart from how to lay all that out, is simple then. Basically have a Cities, States and Countries table in your DB, then create models something like:

class Country
  has_many :states
end

class State
  belongs_to :country
  has_many :cities
end

class City
  belongs_to :state
end

EDIT: To associate it with users: Assuming that a user can only belong to one city (although multiple would not be that difficult either), it would look something like:

class User 
   belongs_to :city 
end 

class City
   belongs_to :state
   has_many :users 
end 

You should be able to do things like:

usr = User.find(a_user_id) 
usr.city                 #New York City 
usr.city.state           #New York 
usr.city.state.country   #United States


Perhaps something like this:

http://www.geonames.org/export/

is what you're looking for?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜