Home > Blockchain >  Can I get the time from the status change without adding additional column or using updated_at?
Can I get the time from the status change without adding additional column or using updated_at?

Time:05-12

How to detect attribute changes from model? Relating to this question, can i see the time when the status is changed? please guide and help.

CodePudding user response:

You can utilize rails Active Model Dirty to check if a particular column in the model changed.

For example, image a Car class with a column status that you would like to observe. As Rails auto-generates the method status_changed?, you can use this as a conditional check to execute your custom method and read the time.

class Car
  before_update :my_custom_method, if: :status_changed?

  def my_custom_method
    # read the time
  end
end
  • Related