首页 > 解决方案 > 更新 ApplicationRecord 上的 self 属性在生产中不起作用

问题描述

导轨版本:5

我有一个这样的 ApplicationRecord 模型:

class Foo < ApplicationRecord
  validates :state, presence: true

  def update_state(state)
    self.state = state
    self.last_run_at = Time.now

    if state != state_was
      Rails.logger.info { "DashboardSchedule #{id} state changed from #{state_was} to #{state}"}
    end

    NotifyService.run(self)

    if changed?
      save!
    end
  end
end

NotifyService定义为:

class NotifyService
  def run(foo)
    p foo.state
  end
end

当我运行以下代码时:

a = Foo.find(1)
p a.state # "new"
a.update_state("passing")

在开发中,我会passing在服务中打印出来。但是,在生产中,我会new打印出来。我无法弄清楚这里可能发生了什么,如果它与将参数命名为update_state与类属性相同的冲突有关,或者它是否是模型的一些奇怪的缓存?

标签: ruby-on-railsrails-activerecord

解决方案


推荐阅读