gpt4 book ai didi

ruby-on-rails - 即使 before_save 将其设置为 nil,值也会被设置

转载 作者:行者123 更新时间:2023-12-04 15:21:04 25 4
gpt4 key购买 nike

我正在使用 aasm_state gem 和 sidekiq。这是customer.rb 中aasm_state 定义的样子:

  aasm do
state :closed, initial: true
state :open
state :claimed

event :open do
transitions from: :closed, to: :open
end

event :claim do
transitions from: [:open, :closed], to: :claimed
end

event :close do
transitions from: [:open, :claimed], to: :closed
before do
self.user_id = nil
end
end
end

然后我在 customer.rb 中也有:
  def properly_close
if closed? && user_id
Rails.logger.info "Bad customer state with customer_id: #{id} at #{Time.now} and the last message was at #{messages.last.created_at}. Aasm_state_was: #{aasm_state_was}"
self.user_id = nil
end
end

每当 aasm_state == 'closed' 时,客户就不应该有 user_id。但是,它仍然经常发生。我认为它与并行的 sidekiq 工作有关,但我不确定。即使使用我的两种方法来确保 user_id = nil(在 before do 和 in correct_close 中),它仍然最终设置为 aasm_state == 'closed' && user_id

这怎么可能?我如何弄清楚它是如何发生的?

最佳答案

在这两种情况下,您都需要 save更新,即:

self.user_id = nil
self.save

或者更简洁(并跳过回调):
self.update_attribute :user_id, nil

关于ruby-on-rails - 即使 before_save 将其设置为 nil,值也会被设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31063227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com