This project is archived and is in readonly mode.

#6481 new
Hans Engel

ActiveRecord: PostgreSQL floats returned as Strings; SQLite floats returned as floats

Reported by Hans Engel | February 27th, 2011 @ 04:23 PM

I've been working on a Rails app and just switched over to PostgreSQL for use on Heroku. The view for the index of my site calls the round method for some float values returned from the database. This worked fine when I was using SQLite — ActiveRecord returned Ruby Floats for columns that held float values in the database. I switched to PostgreSQL and got this error, however, after migrating / seeding the database and visiting the homepage:

2011-02-27T08:14:00-08:00 app[web.1]: ActionView::Template::Error (undefined method `round' for "86.0000000000000000":String):
2011-02-27T08:14:00-08:00 app[web.1]:     16:       %tr
2011-02-27T08:14:00-08:00 app[web.1]:     17:           %td= link_to p[0].layout + " / " + p[0].keyboard, :controller => 'profiles', :action => 'view', :id => p[0].id
2011-02-27T08:14:00-08:00 app[web.1]:     18:           %td= p[1]
2011-02-27T08:14:00-08:00 app[web.1]:     19:           %td= p[2].round
2011-02-27T08:14:00-08:00 app[web.1]:     20:           %td= p[3].round
2011-02-27T08:14:00-08:00 app[web.1]:   app/views/home/index.haml:19:in `_app_views_home_index_haml__1618257786_23737684188900_0'
2011-02-27T08:14:00-08:00 app[web.1]:   app/views/home/index.haml:15:in `each'
2011-02-27T08:14:00-08:00 app[web.1]:   app/views/home/index.haml:15:in `_app_views_home_index_haml__1618257786_23737684188900_0'

I'm guessing this is a bug in how ActiveRecord handles PostgreSQL data types?

Comments and changes to this ticket

  • Utkarsh Kukreti

    Utkarsh Kukreti February 27th, 2011 @ 09:15 PM

    What do you have in your schema.rb? A similar piece of code works fine for me on Heroku/PostgreSQL

  • Hans Engel

    Hans Engel February 27th, 2011 @ 09:19 PM

    This is the relevant table in schema.rb:

      create_table "records", :force => true do |t|
        t.integer  "user_id"
        t.integer  "profile_id"
        t.integer  "words"
        t.integer  "duration"
        t.integer  "wpm"
        t.integer  "cpm"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
    

    The columns wpm and cpm were being returned as Strings. Maybe because I was using an integer type in my migrations instead of specifying a float type?

  • Utkarsh Kukreti

    Utkarsh Kukreti February 28th, 2011 @ 10:59 AM

    Yes. That should have been t.float. I'm guessing the data was imported as-is into PostgreSQL, which made the fields behave like String, as they had an invalid value for their column. Could you try again with t.float?

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages