开发者

Does the update_all method in ActiveRecord return the number of rows?

开发者 https://www.devze.com 2023-02-13 10:42 出处:网络
I want to set the \'lock\' column on a row only if it is not already set.Race conditions are likely and performance is important so this must be done in a single query.I think the solution should look

I want to set the 'lock' column on a row only if it is not already set. Race conditions are likely and performance is important so this must be done in a single query. I think the solution should look like this:

开发者_开发问答class MyModel
  def lock(worker)
    cnt = MyModel.where(id: self.id, lock: nil).update_all(:lock=>worker.name)
    cnt == 1
  end
end

Does the update_all method actually return the number of rows effected like it does in DataMapper?


Yes, it does in ActiveRecord as in DataMapper

0

精彩评论

暂无评论...
验证码 换一张
取 消