This project is archived and is in readonly mode.

#2051 ✓duplicate
Kenny Carruthers

ActiveRecord changes incorrect reports integer column changed when passed a string.

Reported by Kenny Carruthers | February 23rd, 2009 @ 06:18 AM | in 2.x

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

Comments and changes to this ticket

  • Kenny Carruthers

    Kenny Carruthers February 23rd, 2009 @ 06:32 AM

    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

  • Frederick Cheung

    Frederick Cheung May 10th, 2009 @ 03:49 PM

    • State changed from “new” to “duplicate”

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

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>

Pages