Rails does not start my after_commit callback

Hi, I have a peculiar problem. I use the after_commit callback to send notifications, but it looks like the callback is not starting at all. A simplified situation is as follows:

class Message < ActiveRecord::Base after_commit :do_something def do_something raise 'Doing something' end end 

Then I expected to see a raise when I open the console and create a message. But nothing happens. Also, the rails don't even complain if I completely remove the do_something method. Please note that this is not a test with transaction lights. I even see the entry recorded in db. My rails version is 3.0.9. Thanks for any help, especially if it's good :-)

Edit: I later found out that the callback DID is triggered when the code is deployed to another machine. So this should be something with my setup. However, I would appreciate your understanding of what might be causing this.

Edit2: From the comments.

  • The database is MySQL, so transactions are present.
  • Specifying a callback action did not help (: on =>: create).
  • I need after_commit and no other callback
+6
source share
5 answers

David mentioned the explanation of this behavior in the commentary on the comments.

Transaction Processing Documentation

“If any exceptions occur in one of these callbacks, they should be ignored so that they do not interfere with the other callbacks. For example, if your callback code can throw an exception, you will need to save it and handle it properly in the callback. "

see also:

+3
source

You may need to add a patch to the specification / support to enable after_commit callbacks when using transactional fixtures. If you use transaction locks, then commit will never actually happen.

Patch: https://gist.github.com/charleseff/1305285

+1
source

You must specify the action when this after_commit should be run ....

After_commit is mainly used in those Databases where the concept of transaction is present ... So, try the following code ...

 class Message < ActiveRecord::Base after_commit :do_something, :on => [:create, :update, :destroy] def do_something raise 'Doing something' end end 
0
source

Try if the code below helps -

 class Message < ActiveRecord::Base after_commit :do_something, :only => [:create, :update, :destroy] def do_something raise 'Doing something' end end 
0
source

You have the following:

 class Message < ActiveRecord::Base after_commit :do_something def do_something puts 'Doing something' end end 

You can try with after_create or after_save depending on your needs.

after_commits are simply executed when the record is successfully saved to the database, because save, save! and destroy the traces inside the transaction.

As stated in the documentation: Active callbacks

-2
source

All Articles