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 changes incorrect reports integer column changed when passed a string.

#2051

An ActiveRecord object will incorrectly (IMHO) report that an integer column's value has changed if you pass it a string representing of its existing value.

Rails v2.2.2

ex:


class Match < ActiveRecord::Base
  # column 'score' defined as integer with value 5
  # No validations, accessors or anything
end

m = match.first
m.score
# 5

m.score = 5
m.score_changed?
# false

m.score = "5"
m.score_changed? 
# true
m.score
# 5

This issues comes up most for me when forms are passing values back to the controller via the params hash. Since the values in the params hash are all strings, false changes are recorded if the value of an integer column did not actually change.

Thank You, Kenny Carruthers kenny@kennyc.com

Reported by Kenny Carruthers · February 23rd, 2009 @ 06:18 AM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Kenny Carruthers
    Kenny Carruthers

    Note, that after a bit more investigating, this actually only appears to happen when the value of the column is 0.

    So the above example should be re-written as follows:

    
    m = match.first
    m.score
    # 0
    
    m.score = 0
    m.score_changed?
    # false
    
    m.score = "0"
    m.score_changed? 
    # true
    m.score
    # 0
    

    Sorry for the mixup.

    Kenny

    February 23rd, 2009 @ 06:32 AM

  2. Frederick Cheung
    Frederick Cheung
    • State changed from new to duplicate

    This is the same issue as #1530 ActiveRecord::Dirty incorrect behaviour which was fixed (grab that patch or update to 2.3.2)

    May 10th, 2009 @ 03:49 PM