Ruby on rails 在我从循环外部进行检查之前,循环内的Rails乐观锁定更新似乎有效

Ruby on rails 在我从循环外部进行检查之前,循环内的Rails乐观锁定更新似乎有效,ruby-on-rails,optimistic-locking,Ruby On Rails,Optimistic Locking,我在Rails模型上使用乐观锁定。在一个循环中,我更新并保存这个模型,或者更确切地说,保存这个模型的许多实例 从循环内部,我输出before和after值,字段似乎已正确更新。但是之后,当我按ID查找模型时,我看到字段没有更新。有人能发现我的错误吗 class Usage::LeadDistributionWeight < ActiveRecord::Base attr_accessible :agent_id, :tag_value_id, :weight, :premium_lim

我在Rails模型上使用乐观锁定。在一个循环中,我更新并保存这个模型,或者更确切地说,保存这个模型的许多实例

从循环内部,我输出before和after值,字段似乎已正确更新。但是之后,当我按ID查找模型时,我看到字段没有更新。有人能发现我的错误吗

class Usage::LeadDistributionWeight < ActiveRecord::Base
  attr_accessible :agent_id, :tag_value_id, :weight, :premium_limit, :countdown, :lock_version, :tag_value

  def increment_countdown!
    self.countdown = self.countdown + self.weight
    save
  rescue ActiveRecord::StaleObjectError
    attempts_to_crement_countdown ||= 0
    attempts_to_crement_countdown += 1
    self.increment_countdown! unless attempts_to_crement_countdown > 5
    false
  end

  def self.increment_countdowns parent_id, lead_type_id
    if lead_type_id.present?
      joins(:agent)
      .where("#{reflect_on_association(:agent).table_name}.parent_id = ?", parent_id)
      .where(tag_value_id:lead_type_id)
      .all(readonly:false).each { |weight|
        prev = weight.countdown
        if weight.increment_countdown!
          puts "#{prev} differs from #{weight.countdown}"
        else
          puts "no difference!"
        end
      }
    end
  end
end