Skip to content

Instantly share code, notes, and snippets.

@quolpr
Created July 16, 2017 11:15
Show Gist options
  • Save quolpr/12d6ef63690fcb54f1e7da30cb40838b to your computer and use it in GitHub Desktop.
Save quolpr/12d6ef63690fcb54f1e7da30cb40838b to your computer and use it in GitHub Desktop.
Trnasactions

Если вызывать CreateComment.(comment_params), то всё будет ок и sidekiq найдёт нужную запись в базе и выполнит задачу.

НО! Если выполним CreatePost.(post_params), то CommentNotificaitonWorker обвалится, т. к. CommentNotificaitonWorker будет вызываться в незакомиченной родительской транзакции CreatePost

class CreatePost
def call
in_transaction do
Post.create!(params[:post]).tap do |post|
params[:comments].each do |comment_params|
post.comments << CreateComment.(comment_params)
end
end
end
end
end
class CreateComment
def call
comment = in_transaction do
Comment.create!(params)
end
CommentNotificaitonWorker.perform_later(comment.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment