Use of xxx_changed? and xxx_was methods in Rails 🙌🏼

In Ruby/Rails, while you modify any attribute and compare it in observer, instead of using changes method you can use xxx_changed? and xxx_was methods.

Code snippet 📌

user = User.create(name: "Rishi", is_active: false)
user.is_active = true

# Comparing using changes method
if user.changes.present? && user.changes[:is_active].present? && user.changes[:is_active][1] == true
  puts "User got activated"

# Comparing using xxx_changed method
if user.is_active_changed? && user.is_active
  puts "User got activated"

# xxx_was method will return previous value
user.is_active_was # returns false