是否有任何方法来选择一个ActiveRecord关系更新?



在Rails 6.1中,我想选择一个关系进行更新(锁定事务中的行)。

Foo.transaction do
# this is foos_query in raw sql
ActiveRecord::Base.connection.execute <<~SQL
SELECT FROM "foos"
WHERE
type = 'bar' AND
associated_object = '#{thing_id}' AND
other_party_id = '#{user_id}'
ORDER BY id
FOR UPDATE
SQL
foos_query.update_all(read: "true", seen: true)
end

在以前的rails版本中,我认为可以被foos_query.lock.pluck('')攻击,但是它在某个时候停止工作。

是否有一个支持或黑客的方式来做到这一点,没有原始sql?

相关:

  • 我怎么能有rails选择更新多行?
  • https://dba.stackexchange.com/questions/257188/how-can-i-lock-rows-for-update-without-returning-data-over-the-connection

我选择了

ActiveRecord::Base.connection.execute foos_query.order(:id).lock.to_sql

参见

https://dba.stackexchange.com/questions/257188

https://dba.stackexchange.com/questions/257217

相关内容

最新更新