Posts Tagged ‘geolocation’

Dead simple IP to Country in Rails

Saturday, December 1st, 2007

Step 1:

Follow these instructions to get MaxMind’s GeoLite IPCountry table into your Rails MySQL database.

Step 2:

In a Rails Controller:

  def get_country_from_ip
    ip = request.remote_ip
    result = ActiveRecord::Base.connection.execute("SELECT country_code FROM ip_country WHERE MBRCONTAINS(ip_poly, POINTFROMWKB(POINT(INET_ATON('#{ip}'), 0)))")
    result.fetch_row
  end

Step 3:

There’s no step 3. There’s no step 3! Ahahahaa!