Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#6481

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?

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Utkarsh Kukreti
    Utkarsh Kukreti

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

    February 27th, 2011 @ 09:15 PM

  2. Hans Engel
    Hans Engel

    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?

    February 27th, 2011 @ 09:19 PM

  3. Utkarsh Kukreti
    Utkarsh Kukreti

    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?

    February 28th, 2011 @ 10:59 AM