Skip to content

Instantly share code, notes, and snippets.

@amolpujari
Created October 18, 2012 06:06
Show Gist options
  • Save amolpujari/3910130 to your computer and use it in GitHub Desktop.
Save amolpujari/3910130 to your computer and use it in GitHub Desktop.
Location model example
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > l = Location.new 'San Francisco'
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: "US", state: "California", state_abbreviation: "CA", county: nil, city: "San Francisco", street: nil, address: "San Francisco", zip: "94103", lat: #<BigDecimal:6ea81b0,'0.37775002E2',18(45)>, lng: #<BigDecimal:6ea7f58,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :002 > ap l
#<Location:0x00000006b82e18> {
:id => nil,
:locatable_id => nil,
:locatable_type => nil,
:ip => nil,
:country => "US",
:state => "California",
:state_abbreviation => "CA",
:county => nil,
:city => "San Francisco",
:street => nil,
:address => "San Francisco",
:zip => "94103",
:lat => 37.775002,
:lng => -122.418297,
:walkscore => nil,
:transit_score => nil,
:avg_school_rating => nil,
:created_at => nil,
:updated_at => nil
}
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: "US", state: "California", state_abbreviation: "CA", county: nil, city: "San Francisco", street: nil, address: "San Francisco", zip: "94103", lat: #<BigDecimal:70b5a98,'0.37775002E2',18(45)>, lng: #<BigDecimal:70b5868,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :003 >
1.9.3p194 :004 > l = Location.new :lat => 37.775002, :lng => -122.418297
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: nil, state: nil, state_abbreviation: nil, county: nil, city: nil, street: nil, address: nil, zip: nil, lat: #<BigDecimal:70c4188,'0.37775002E2',18(45)>, lng: #<BigDecimal:70c3f30,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :005 > ap l
#<Location:0x000000070bd7c0> {
:id => nil,
:locatable_id => nil,
:locatable_type => nil,
:ip => nil,
:country => nil,
:state => nil,
:state_abbreviation => nil,
:county => nil,
:city => nil,
:street => nil,
:address => "",
:zip => nil,
:lat => 37.775002,
:lng => -122.418297,
:walkscore => nil,
:transit_score => nil,
:avg_school_rating => nil,
:created_at => nil,
:updated_at => nil
}
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: nil, state: nil, state_abbreviation: nil, county: nil, city: nil, street: nil, address: nil, zip: nil, lat: #<BigDecimal:6dd13e0,'0.37775002E2',18(45)>, lng: #<BigDecimal:6dd11b0,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :006 > l.zip(true)
=> "94103"
1.9.3p194 :007 > ap l
#<Location:0x000000070bd7c0> {
:id => nil,
:locatable_id => nil,
:locatable_type => nil,
:ip => nil,
:country => "US",
:state => "California",
:state_abbreviation => "CA",
:county => nil,
:city => "San Francisco",
:street => nil,
:address => "San Francisco, California, 94103",
:zip => "94103",
:lat => 37.775002,
:lng => -122.418297,
:walkscore => nil,
:transit_score => nil,
:avg_school_rating => nil,
:created_at => nil,
:updated_at => nil
}
=> #<Location id: nil, locatable_id: nil, locatable_type: nil, ip: nil, country: "US", state: "California", state_abbreviation: "CA", county: nil, city: "San Francisco", street: nil, address: nil, zip: "94103", lat: #<BigDecimal:7077ec8,'0.37775002E2',18(45)>, lng: #<BigDecimal:7077c98,'-0.122418297E3',18(45)>, walkscore: nil, transit_score: nil, avg_school_rating: nil, created_at: nil, updated_at: nil>
1.9.3p194 :008 >
@amolpujari
Copy link
Author

Once you create a new location object by setting lat, lng, make a call to zip(true), which will make it fetch details and reverse geocoding takes place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment