How to force Rails ActiveRecord to commit a transaction flush

ActiveRecord uses #transaction to create a block that begins and either rolls back or commits a transaction. I believe that would help your issue. Essentially (presuming Task is an ActiveRecord class):

Task.transaction do
  new_task = Task.create(...)
end

BackgroundQueue.enqueue(new_task)

You could also go directly to the #connection underneath with:

Task.connection.commit_db_transaction

That's a bit low-level, though, and you have to be pretty confident about the way the code is being used. #after_commit is the best answer, even if it takes a little rejiggering of the code to make it work. If it won't work for certain, then these two approaches should help.