Reputation: 41
I can't catch the changed attributes on after_save callback but I can catch them on after_update callback. I think after_save is just a combination of after_create and after_update. I'd appreciate it if someone give me at least a hint.
class Student < ApplicationRecord
after_save: after_save_callback
def after_save_callback
if username_changed? ### This is always false
# do something
end
end
end
student = Student.create(name: 'John Doe', username: 'abcdef')
student.username = '123456'
student.save
My Rails version is 5.0.7.
Upvotes: 4
Views: 1713
Reputation: 31
You can use saved_change_to_username?
The behavior of attribute_changed?
inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after save
returned (e.g. the opposite of what it returns now). To maintain the current behavior, use saved_change_to_attribute?
instead.
Upvotes: 3