outside_transaction
Makes sure the given block is only executed when not inside a transaction. If there is an open transaction it will be executed after the next successful commit or not executed at all if there is a rollback or failure.
This is useful when you're notifying another process. If the other process starts processing the message before the transaction is actually committed, it will not see your uncommitted changes as transactions are isolated (usually).
In the example below, if there is a lot of work after saving and
article, and ArticleEmailSender
is started too soon, it will not find
the article you just inserted.
Example:
class Article
after_create :send_emails
def send_emails
outside_transaction do
Resque.enqueue(ArticleEmailSender, id) # notifies another process
end
end
end
This example is using Resque.