Я хочу написать фрагмент кода таким образом, чтобы гарантировалось , что в любой момент времени только один процесс может обновить поле для определенной записи в таблице сообщений.
Это правильный способ сделать это?
#Make a check before entering transaction, so that a transaction
#is not entered into needlessly (this check is just for avoiding
#using DB resources that will be used when starting a transaction)
if @post.can_set_to_active?
ActiveRecord::Base.transaction do
#Make a check again, this time after entering transaction, to be
#sure that post can be marked active.
#Expectation is that inside a transaction, it is guaranteed that no other
#process can set the status of this post.
if @post.can_set_to_active?
#now set the post to active
@post.status = :active
@post.save
end #end of check inside transaction
end #end of transaction
end #end of check outside transaction
Кроме того, есть ли способ проверить этот сценарий с использованием RSpec или даже каким-либо другим методом?