'Rails ActiveRecord dirty: saved_change_to_attribute? from not nil to not nil

In my model I have a start attribute which is a dateTime field

Using an active_record callback I have the following logic:

if saved_change_to_attribute?(:start, from: nil)
  # logic goes here

Now I need to factor for another scenario where the start attribute has changed from one date to another. How would I capture a change which IS Not from nil to populated?



Solution 1:[1]

You can use the attribute_was, attribute_change, attribute_previously_was properties.

attribute_change will give you an array of previous and current value.

attribute_was and attribute_previously_was will give you last value.

instance.start_change # -> [nil, '10/10/21]
instance.start_was # -> nil

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hiader Ali