I use the after_update callback in my User model.
Model User.rb
after_update :check_phone
check_phone
phone_validation if phone_changed?
end
def phone_validation
code = Array.new(8){rand(36).to_s(36)}.join
self.phone_verification_code = code
self.save
end
However, this leads to an endless loop. The problem is that the callback is called again after self.save
in the phone_validation method. phone_changed?
apparently still returns true
. How can I change this behaviour?
In your case
self.save
triggers the update which triggers theafter_update
again ending in an endless loop.Try giving before_update instead of
after_update
Try:
Hope it helps :)
Try this:
User.rb
OR
use with caution: skip callbacks