This project is archived and is in readonly mode.

#798 ✓resolved
seanliugm (at gmail)

reverted changes should not be marked as changed

Reported by seanliugm (at gmail) | August 11th, 2008 @ 10:24 AM | in 2.x

attribute_changed? is true even though the value of attribute is changed back to its original value, for example,


>> post = Post.first
=> #<Post id: 1, title: "old", published_at: nil, created_at: "2008-07-30 09:41:03", updated_at: "2008-08-11 08:47:29">
>> post.title
=> "old"
>> post.changed?
=> false
>> post.title_changed?
=> false
>> post.title = 'new'
=> "new"
>> post.changed?
=> true
>> post.title_changed?
=> true
>> post.title_change
=> ["old", "new"]
>> post.title = 'old'
=> "old"
>> post.changed?
=> true
>> post.title_changed?
=> true
>> post.title_change
=> ["old", "old"]

In my opinion, 'title' should not be marked as changed anymore after its value is changed back to the original value. And i think the following reimplementation of method "write_attribute_with_dirty" will solve this issue:


module ActiveRecord
  module Dirty
    private
    def write_attribute_with_dirty(attr, value)
         attr = attr.to_s

         # The attribute already has an unsaved change.
         unless changed_attributes.include?(attr)
           old = clone_attribute_value(:read_attribute, attr)
           changed_attributes[attr] = old if field_changed?(attr, old, value)
         else
           old = changed_attributes[attr]
           changed_attributes.delete(attr)  unless field_changed?(attr, old, value)
         end

         # Carry on.
         write_attribute_without_dirty(attr, value)
    end
  end
end

Comments and changes to this ticket

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>

Attachments

Referenced by

Pages