Allowing saved_change_to_attribute? to accept multiple attributes

I’m writing some ActiveRecord callbacks, which need to check a few attributes for changes.

after_save :do_work

def do_work
  return unless saved_change_to_name? || saved_change_to_description? || saved_change_to_favorite_superhero?

  # Important code...
  # ...
end

It looks pretty wordy, and feels like Rails could support saved_changes_to_any_attribute?(:name, :description, :favorite_superhero), and possibly saved_changes_to_attributes?(:name, ...) to detect a change to all.

I’d be happy to make a PR for this, if it seems like a useful addition?

Thanks!

1 Like

This would be a great addition.

I “solved” this by using saved_changes.keys and checking if the keys match any properties from an array of keys I use as a whitelist.

In your case you can define an array of %w(name description superhero) in the model. And then in your code, have an if that checks if <model>.saved_changed.keys.any? {|property| CONSTANT.include?(property) }

This would be a great feature though.