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.

Added previous_changes to ActiveRecord::Dirty [PATCH]

#2650

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

Reported by Scott Barr · May 15th, 2009 @ 05:24 AM

State: committed
Milestone: 3.0.2
Assigned to: josh josh
Importance: none

Activity

  1. Scott Barr
    Scott Barr

    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 # => {}
    

    May 15th, 2009 @ 05:50 AM

  2. Scott Barr
    Scott Barr
    • Tag changed from patch to activerecord, dirty, patch

    May 15th, 2009 @ 12:57 PM

  3. orangechicken
    orangechicken

    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.

    July 7th, 2009 @ 11:18 PM

  4. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, dirty, patch to activerecord, bugmash, dirty, patch

    August 7th, 2009 @ 02:25 PM

  5. Josh Sharpe
    Josh Sharpe

    This patch doesn't apply cleaning for me. I like the idea though.

    August 8th, 2009 @ 03:25 AM

  6. pjammer
    pjammer

    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.

    August 8th, 2009 @ 03:43 AM

  7. Scott Barr
    Scott Barr

    I'll clean up the patch and resubmit.

    August 8th, 2009 @ 02:11 PM

  8. Greg Sterndale
  9. Greg Sterndale
  10. Greg Sterndale
  11. David Trasbo
    David Trasbo

    The patch does not apply cleanly to edge since activerecord/lib/active_record/dirty.rb has been moved.

    August 8th, 2009 @ 10:37 PM

  12. Nick Quaranto
    Nick Quaranto
      1. 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.

    August 8th, 2009 @ 11:19 PM

  13. Josh Nichols
    Josh Nichols

    +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.

    August 8th, 2009 @ 11:23 PM

  14. Greg Sterndale
  15. Greg Sterndale
  16. Jeremy Kemper
    Jeremy Kemper
    • Assigned user set to Jeremy Kemper
    • State changed from new to open
    • Milestone changed from 2.x to 2.3.4
    1. Nice way to track an undo history in an after_save. Needs rebase against master.

    August 9th, 2009 @ 01:29 AM

  17. Michael Koziarski
    Michael Koziarski

    Adding Josh as he's looking at this in amo

    August 9th, 2009 @ 03:07 AM

  18. David Trasbo
  19. Matt Jones
    Matt Jones

    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.

    August 9th, 2009 @ 06:29 PM

  20. josh
    josh
    • Assigned user changed from Jeremy Kemper to josh
    • Milestone cleared.

    August 10th, 2009 @ 02:31 AM

  21. josh
    josh

    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.

    August 10th, 2009 @ 02:33 AM

  22. josh
    josh
    • State changed from open to incomplete

    August 19th, 2009 @ 03:58 PM

  23. Scott Barr
    Scott Barr

    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.

    August 19th, 2009 @ 04:04 PM

  24. Josh Sharpe
    Josh Sharpe
    • 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.

    August 31st, 2009 @ 05:36 PM

  25. josh
  26. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM