Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 阅读锁模型_Ruby On Rails_Ruby_Rails Activerecord - Fatal编程技术网

Ruby on rails 阅读锁模型

Ruby on rails 阅读锁模型,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,在ActiveRecord事务中,我需要某种方法来锁定记录/表进行读取,因此在执行事务时,用户无法从此表中进行选择 基本上,我需要这样的东西 System.transaction do s = System.first # Something to lock the table or selected record for reading s.update_attributes(some_params) end 有人知道怎么做吗?看来你正在寻找。您可以像使用范围一样使用lock方法

ActiveRecord
事务中,我需要某种方法来锁定记录/表进行读取,因此在执行事务时,用户无法从此表中进行选择

基本上,我需要这样的东西

System.transaction do
  s = System.first
  # Something to lock the table or selected record for reading
  s.update_attributes(some_params)
end

有人知道怎么做吗?

看来你正在寻找。您可以像使用范围一样使用
lock
方法。此外,您应该使用bang版本
update\u属性以便在出现问题时引发异常

System.transaction do
  s = System.lock.first
  s.update_attributes!(some_params)
end

为什么需要锁定读取?也许有更好的办法。例如,Postgres对整个表有读锁,但对行没有读锁。我需要在更改变量时锁定此记录,以避免冲突。尽管如此,如果你能想出更好的方法,我还是愿意接受更好的建议。这个东西对MySql有用吗?锁定后我仍然能够读取记录。再说一遍,为什么锁定读取?锁定要更改的资源以进行更新,而不是某些状态变量以进行读取!