The value is set even if before_save sets it to zero.

I use aasm_state stone, along with sidekiq. Here is what customer.rb looks like for aasm_state definition:

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 

and then I also have client.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 

Whenever aasm_state == 'closed', the user should never be user_id for the client. However, this still happens, often. I think this has something to do with sidekiq sites in parallel, but I'm just not sure. Even with my two ways to make sure that user_id = nil (in before and in right_close), it still ends up being set using aasm_state == 'closed' && & user_id

How is this possible? How do I understand how this happens?

+6
source share
1 answer

In both cases, you need to save update, i.e.

 self.user_id = nil self.save 

Or more succinctly (and skipping callbacks):

 self.update_attribute :user_id, nil 
+4
source

Source: https://habr.com/ru/post/989775/


All Articles