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.

Dirty Attributes and string manipulation

#5110

Just found what I believe is a bug.

With ActiveRecord modify the value of a string field with

@project.title

"HelloWorld"

@project.title[0] = "h"

"helloWorld"

@project.changed?

false

This means that saving the record does not write the change.

Work around is

title = @project.title.clone

title[0] = "h"

@project.title = title

@project.changed?

true

Reported by Lee Irving · July 14th, 2010 @ 09:34 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ivan Torres (mexpolk)
    Ivan Torres (mexpolk)
    • Tag changed from rails 2.3.8, activerecord, dirty to rails 2.3.8, activerecord, dirty, rails3

    You're right, the strangest thing is that the original value is lost:

    @project.title_was => "helloWorld"

    Also this affects Rails 3

    July 14th, 2010 @ 03:11 PM

  2. Ivan Torres (mexpolk)
    Ivan Torres (mexpolk)

    Doing some tests it turns out that if the String instance is the same, then the change is not detected. If the string is another instance it works as expected:

      # This wont work
      @project.title
      => "Rails"
      @project.title << " test"
      => "Rails test"
      @project.title_was
      => "Rails test"
      @project.title_changed?
      => false
    
      # This works
      @project.reload
      @project.title += " test"
      => "Rails test"
      @project.title_was
      => "Rails"
      @project.title_changed?
      => true
    

    July 14th, 2010 @ 04:54 PM

  3. Neeraj Singh
    Neeraj Singh
    • State changed from new to invalid
    • Importance changed from to Low

    July 29th, 2010 @ 02:19 PM