当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Lock model for reading

问题描述:

In an ActiveRecord transaction, I need some way to lock the record/table for reading, so the user was not able to select from this table, while the transaction being executed.

Basically, I need something like this

System.transaction do

s = System.first

# Something to lock the table or selected record for reading

s.update_attributes(some_params)

end

Does anyone have an idea on how to do that?

网友答案:

It seems you are looking for Pessimistic Locking. You can use the lock method like a scope. Additionally, you should use the bang-version update_attributes! in order to raise an exception when things go wrong.

System.transaction do
  s = System.lock.first
  s.update_attributes!(some_params)
end
分享给朋友:
您可能感兴趣的文章:
随机阅读: