RPV
RPV

Reputation: 397

How to access previous updated_at?

I have model in which I keep track of the field updated_at.

Is there a way I can track the previous updated_at?

For example

updated_at = A (where A is an actual datetime stamp)

Some work is done then save is called

updated_at = B (where B is an actual datetime stamp)

Is there a way I can access the previous updated_at i.e. A?

Upvotes: 2

Views: 950

Answers (3)

Abdulrazak Alkl
Abdulrazak Alkl

Reputation: 923

It will be useful if you use paper trail gem. It will persist the previous record on disk which may affect your app performance. Better way to implement an in-memory solution to store the previous records.

Upvotes: 3

Jenorish
Jenorish

Reputation: 1714

Please have a look ActiveModel::Dirty module:

object.updated_at           # returns current value
object.updated_at_changed?  # returns true or false if value has changed
object.updated_at_was       # return last value
object.updated_at_change    

Or

If you wants to track all changed values you can use Paper Trail Gem.

Upvotes: 5

dp7
dp7

Reputation: 6749

If you have the object available, then you can call:

object.previous_changes

This would return you a hash as follows showing which attributes have been changed:

{"name"=>["foo", "bar"], "updated_at"=>[Tue, 19 Apr 2016 08:19:40 UTC +00:00, Mon, 25 Apr 2016 10:49:47 UTC +00:00]} 

Refer: previous_changes

Upvotes: 5

Related Questions