ActiveRecord中的update_all方法是否返回行数?

Joh*_*ler 5 activerecord ruby-on-rails ruby-on-rails-3

我想在行上设置"锁定"列,如果它尚未设置的话.竞争条件很可能并且性能很重要,因此必须在单个查询中完成.我认为解决方案应该如下所示:

class MyModel
  def lock(worker)
    cnt = MyModel.where(id: self.id, lock: nil).update_all(:lock=>worker.name)
    cnt == 1
  end
end
Run Code Online (Sandbox Code Playgroud)

update_all方法实际上是否像在DataMapper中那样返回受影响的行数?

fl0*_*00r 6

是的,它在ActiveRecord中和DataMapper一样