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.

reverted changes should not be marked as changed

#798

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

Reported by seanliugm (at gmail) · August 11th, 2008 @ 10:24 AM

State: resolved
Milestone: 2.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Tom Lea
    Tom Lea
    • Tag set to activerecord, patch

    +1 easy pitfall fixed

    Patch to do this attached, changed format slightly.

    August 11th, 2008 @ 02:16 PM

  2. seanliugm (at gmail)
    seanliugm (at gmail)
    • Title changed from attribute_changed? is true even though the value of attribute is changed to original value to reverted changes should not be marked as changed

    August 12th, 2008 @ 09:24 AM

  3. Pratik
    Pratik
    • Assigned user set to Jeremy Kemper

    August 12th, 2008 @ 02:07 PM

  4. Ryan Bigg
  5. Damian Janowski
    Damian Janowski

    Can somebody paste a real world example?

    Or is this to make your irb sessions "faster"?

    August 21st, 2008 @ 05:58 AM

  6. Tom Lea
    Tom Lea

    Example: Unpaid users can only store a maximum of 300 points, others can store more.

    
    MAX_UNPAID_POINTS=300
    user = User.find(session[:user_id])
    user.points += 10 * user.years_has_been_a_member
    user.points = [user.points, MAX_UNPAID_POINTS].min unless user.paid?
    
    update_profile_icon if user.points_changed?
    

    Some people may prefer to do the same thing in different styles, assigning from the result of an if or similar, but this is a perfectly valid approach too.

    August 21st, 2008 @ 10:28 AM

  7. Repository
    Repository
    • State changed from new to resolved

    (from [a3a3067adbd578ad9d145f5a148816ec0460a987]) Dirty: treat two changes resulting in the original value as being unchanged.

    [#798 reverted changes should not be marked as changed state:resolved]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...

    August 28th, 2008 @ 07:21 AM

  8. Repository
    Repository

    (from [ad562c58eabfb8b44cb8ac9e87b87a7f998325fd]) Dirty: treat two changes resulting in the original value as being unchanged.

    [#798 reverted changes should not be marked as changed state:resolved]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...

    August 28th, 2008 @ 07:21 AM