Rails :after_save callback runs before the actual database record is created. So use :after_commit instead

January 10, 2012

I ran into a gotcha yersterday when using the :after_save Rails callback, i hadn’t realized that the :after_save hook gets called before the SQL transaction is commited to the database. I was trying to tell asterisk to reload a queue from the database in the after_save hook using an AMI command but asterisk was not ‘seeing’ the record in the database at the time the reload command was being issued, then i realized that what i needed is called :after_commit

:after_commit is Rails 3 only but it can be used in Rails 2.x apps using the after commit gem

Happy Hacking


comments powered by Disqus