This project is archived and is in readonly mode.
Added previous_changes to ActiveRecord::Dirty [PATCH]
Reported by Scott Barr | May 15th, 2009 @ 05:24 AM | in 3.0.2
I've added a previous_changes Map to ActiveRecord::Dirty
I added this feature so that a Map of changed fields could be
retrieved
after a model had been saved. This is useful in the after_save
callback
when you need to know what fields changed. At present there is no
way
to do this other than have code in the before_save callback that
takes
a copy of the changes Map, which I thought was a bit messy.
Example
person = Person.find_by_name('bob')
person.name = 'robert'
person.changes # => {'name' => ['bob, 'robert']}
person.save
person.changes # => {}
person.previous_changes # => {'name' => ['bob, 'robert']}
person.reload
person.previous_changes # => {}
I've attached a patch with tests
Comments and changes to this ticket
-
Scott Barr May 15th, 2009 @ 05:50 AM
Reformatted the example, sorry about that
person = Person.find_by_name('bob') person.name = 'robert' person.changes # => {'name' => ['bob, 'robert']} person.save person.changes # => {} person.previous_changes # => {'name' => ['bob, 'robert']} person.reload person.previous_changes # => {}
-
Scott Barr May 15th, 2009 @ 12:57 PM
- Tag changed from patch to activerecord, dirty, patch
-
orangechicken July 7th, 2009 @ 11:18 PM
Thank goodness for this! Having the dirty changes blown out after save severely limits the usefulness of the dirty state -- this seems like a good compromise.
-
CancelProfileIsBroken August 7th, 2009 @ 02:25 PM
- Tag changed from activerecord, dirty, patch to activerecord, bugmash, dirty, patch
-
Josh Sharpe August 8th, 2009 @ 03:25 AM
This patch doesn't apply cleaning for me. I like the idea though.
-
pjammer August 8th, 2009 @ 03:43 AM
This patch says the following when applied with git am :
/home/path/rails/.git/rebase-apply/patch:84: trailing whitespace. error: activerecord/lib/active_record/dirty.rb: does not exist in index
inspecting these lines it doesn't appear that there is whitespace however.
The concept is a good idea. Shouldn't you be more worried about what is saving in the before_save callback, vs. what was saved after? Just to play devil's advocate.
-
David Trasbo August 8th, 2009 @ 10:37 PM
The patch does not apply cleanly to edge since activerecord/lib/active_record/dirty.rb has been moved.
-
Nick Quaranto August 8th, 2009 @ 11:19 PM
-
- This seems a bit overboard to me. Using
model.#{attribute}_was
has always been enough for me, if you're changing it more than once that sounds like bad logic instead to me. I'd like to see some real use cases of this first.
- This seems a bit overboard to me. Using
-
-
Josh Nichols August 8th, 2009 @ 11:23 PM
+1, I could see this making the dirty a lot more useful.
A use case I would use this for goes something like:
- I have a Debate that has 'state' attribute for the current state (ie new, forfeited, etc) - I want to make notifications when this state changes - I could use an after_save callback to kick off a mailer to which includes both the old value and the new value. -
Jeremy Kemper August 9th, 2009 @ 01:29 AM
- Assigned user set to Jeremy Kemper
- State changed from new to open
- Milestone changed from 2.x to 2.3.4
- Nice way to track an undo history in an after_save. Needs rebase against master.
-
Matt Jones August 9th, 2009 @ 06:29 PM
This may be useful, but at least part of your description is misleading - the changed? and was methods work fine inside after_save, as they aren't reset until after the transaction commits.
-
josh August 10th, 2009 @ 02:31 AM
- Assigned user changed from Jeremy Kemper to josh
- Milestone cleared.
-
josh August 10th, 2009 @ 02:33 AM
Lets put it in 3.0.
Can you please rebase against master. I'm about to make some changes to AR dirty tracking in the next few days. I'm extracting them out to AMo.
-
josh August 19th, 2009 @ 03:58 PM
- State changed from open to incomplete
-
Scott Barr August 19th, 2009 @ 04:04 PM
The original patch wouldn't apply because dirty.rb moved after I submitted the patch.
Let me know what this ticket needs and I'll put it together.
-
Josh Sharpe August 31st, 2009 @ 05:36 PM
- Tag changed from activerecord, bugmash, dirty, patch to activerecord, dirty, patch, previous_changes
- Title changed from Added previous_changes to ActiveRecord::Dirty to Added previous_changes to ActiveRecord::Dirty [PATCH]
Rebased Scott's patch for master.
-
josh August 31st, 2009 @ 06:55 PM
- State changed from incomplete to committed
-
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to
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>