rails/rails — pull request #12763 by sausman (2013-11-04 23:56:32+00:00)
Title: ActiveModel Dirty: Add *_previously_changed?, *_previous_change, *_previously_was, previously_changed?, and previously_changed methods.

When using ActiveRecord callbacks and libraries such as Resque/Sidekiq it is best to use `after_commit` in place of `after_{save,create,update}` to avoid ["Cannot find ModelName with ID=12345"](https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting#cannot-find-modelname-with-id12345) errors.

However, you can't simply switch from `after_{save,create,update}` to `after_commit` if you rely on dirty tracking methods because the changes are reset once the transaction is committed.

The attached commit adds dirty tracking methods to allow `after_commit` callbacks to take advantage of dirty tracking. Here is an example:

```ruby
class User
after_save(if: :email_changed?) do
UserMailer.delay.send_email_change_notice(id, email_was)
UserMailer.delay.send_email_confirmation(id)
end
end
```

becomes...

```ruby
class User
after_commit(if: :email_previously_changed?, on: [:create, :update]) do
UserMailer.delay.send_email_change_notice(id, email_previously_was)
UserMailer.delay.send_email_confirmation(id)
end
end
```

Here is the list of new methods:

* `*_previously_changed?`
* `*_previous_change`
* `*_previously_was`
* `previously_changed?`
* `previously_changed`

I can add tests, but thought I'd get some feedback first.