This project is archived and is in readonly mode.
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
-
Tom Lea August 11th, 2008 @ 02:16 PM
- Tag set to activerecord, patch
+1 easy pitfall fixed
Patch to do this attached, changed format slightly.
-
seanliugm (at gmail) August 12th, 2008 @ 09:24 AM
- 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
-
Pratik August 12th, 2008 @ 02:07 PM
- Assigned user set to Jeremy Kemper
-
Damian Janowski August 21st, 2008 @ 05:58 AM
Can somebody paste a real world example?
Or is this to make your irb sessions "faster"?
-
Tom Lea August 21st, 2008 @ 10:28 AM
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.
-
Repository August 28th, 2008 @ 07:21 AM
- State changed from new to resolved
(from [a3a3067adbd578ad9d145f5a148816ec0460a987]) Dirty: treat two changes resulting in the original value as being unchanged.
[#798 state:resolved]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...
-
Repository August 28th, 2008 @ 07:21 AM
(from [ad562c58eabfb8b44cb8ac9e87b87a7f998325fd]) Dirty: treat two changes resulting in the original value as being unchanged.
[#798 state:resolved]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...
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>
People watching this ticket
Attachments
Tags
Referenced by
- 798 reverted changes should not be marked as changed [#798 state:resolved]
- 798 reverted changes should not be marked as changed [#798 state:resolved]