reverted changes should not be marked as changed
Reported by seanliugm (at gmail) | August 11th, 2008 @ 10:10 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 changed from 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 changed from 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...
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"..
