<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: geo code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 12:12:53 GMT</pubDate>
    <description>DZone Snippets: geo code</description>
    <item>
      <title>Numeric colums for latitude / longitude in Rails 1.2 migrations</title>
      <link>http://snippets.dzone.com/posts/show/3216</link>
      <description>In Rails 1.1.6, "numeric" datatypes didn't work in migrations. This confused a lot of people who wanted to store geographic data (latitude and longitude) for use in Google Maps and the like. Floats worked, but their precision is limited -- you'll lose three or more decimal places of precision if you store the results of a typical geocoding call in a Float column. And don't even think about using strings to store your latitudes/longitudes.&lt;br /&gt;&lt;br /&gt;Fortunately, the numeric datatype problem is fixed in Rails 1.2, and you can now have do this in your migrations:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class CreatePlaces &lt; ActiveRecord::Migration&lt;br /&gt;  def self.up&lt;br /&gt;    create_table :places do |t|&lt;br /&gt;      t.column "lat", :decimal, :precision =&gt; 15, :scale =&gt; 10&lt;br /&gt;      t.column "lng", :decimal, :precision =&gt; 15, :scale =&gt; 10&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.down&lt;br /&gt;    drop_table :places&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;FYI, if you're stuck on Rails 1.1.6, you can use this approach:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class CreatePlaces &lt; ActiveRecord::Migration&lt;br /&gt;  def self.up&lt;br /&gt;    create_table :places do |t|&lt;br /&gt;      t.column :lat, :float &lt;br /&gt;      t.column :lng, :float&lt;br /&gt;    end&lt;br /&gt;    execute("ALTER TABLE places MODIFY lat numeric(15,10);")&lt;br /&gt;    execute("ALTER TABLE places MODIFY lng numeric(15,10);")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.down&lt;br /&gt;    drop_table :places&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;... but that's ugly, database-specific (works on MySQL), and not very DRY.&lt;br /&gt;</description>
      <pubDate>Fri, 29 Dec 2006 14:36:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3216</guid>
      <author>alewis (Andre Lewis)</author>
    </item>
  </channel>
</rss>
