#608 √ invalid
quake wang

Dirty checking is broken for string join

Reported by quake wang | July 13th, 2008 @ 04:14 AM | in 2.x

Rails 2.1:

create_table :topics do |t|

t.string :title

end

class Topic < ActiveRecord::Base

end

in console:

>> t = Topic.find(1)

>> t.title = t.title << "new"

>> t.changed?

=> false

Comments and changes to this ticket

  • Jason Dew

    Jason Dew July 14th, 2008 @ 05:30 PM

    Don't see an easy fix, but a workaround is s/<</+=/

  • Jason Dew
  • Tom Ward

    Tom Ward July 25th, 2008 @ 12:26 PM

    This ticket is invalid. Dirty checking will only currently work if you replace an attribute with a new value, not edit it in place.

    Instead you should do:

    t = Topic.find(1)
    t.title_will_change!
    t.title = t.title << "new"
    t.changed?
    # => true
    
  • Clemens Kofler

    Clemens Kofler July 25th, 2008 @ 02:22 PM

    • → Tag changed from “” to “activerecord”

    I agree with Tom. It is known that all changes that don't go through standard assignment with = will not mark the attribute as changed.

  • quake wang

    quake wang July 26th, 2008 @ 02:26 AM

    I know will_change can resolve this issue as a temp solution, however, how to explain the inconsistent of string / numeric field?

    create_table :topics do |t|

    t.string :title

    t.integer :score

    end

    t = Topic.find(1)

    t.title = t.title.contact(33)

    t.changed?

    1. => false

    t.score = t.score.*(33)

    t.changed?

    1. => true
  • Tom Ward

    Tom Ward July 26th, 2008 @ 10:30 AM

    What does t.title.contact(33) do?

    If you set the title to the same value it was before, changed? will return false.

    If you set it to a new value, it should return true.

  • Pratik

    Pratik July 26th, 2008 @ 03:19 PM

    • → State changed from “new” to “invalid”

    Patch please.

  • Clemens Kofler

    Clemens Kofler July 26th, 2008 @ 03:29 PM

    I've tested this and I can confirm it.

    A little something to think about before someone actually spends time on creating a patch that isn't really necessary:

    With t.title << "new", I would actually think that the title changes. t.title = t.title << "new" looks intriguing to me because it looks like it does the actual assignment twice. IMO you shouldn't "reassign" the variable when using the << operator.

    What would make sense, though, is having something like t.title = t.title + "new" or its short form, t.title += "new". Both of them actually work perfectly fine and t.title_changed? returns true.

    The important question to ask is whether one should use something misleading like t.title = t.title << "new" when there's two valid ways around that even look more logical. One is, as I said, t.title += "new" and the other would be to use t.title_will_change! t.title << "new".

  • Zach Holman

    Zach Holman July 31st, 2008 @ 09:00 PM

    I can confirm this as well.

    There's some discussion on whether something is "changed" if the contents are changed. To me that seems like a valid case. I was working on a serialized array to a model, and if I do something like:

    @

    my_model.serialized_array << 'new pushed element'

    my_model.changed # intuitively this should return ["serialized_array"]

    @

    I feel that this is a common scenario where a serialized array needs modification, and the very idea of something being modified is, well, a change.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins