This project is archived and is in readonly mode.

#5110 ✓invalid
Lee Irving

Dirty Attributes and string manipulation

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

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

Comments and changes to this ticket

  • Ivan Torres (mexpolk)

    Ivan Torres (mexpolk) July 14th, 2010 @ 03:11 PM

    • 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

  • Ivan Torres (mexpolk)

    Ivan Torres (mexpolk) July 14th, 2010 @ 04:54 PM

    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
    
  • Neeraj Singh

    Neeraj Singh July 29th, 2010 @ 02:19 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

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