Reputation: 8631
I am using the aasm_state gem, along with sidekiq. Here's what customer.rb looks like for the definition of 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
and then I also have in 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
Whenever aasm_state == 'closed', there should never be a user_id on customer. However, it still happens, frequently. I'm thinking it has something to do with sidekiq jobs in parallel but i'm just not sure. Even with my two ways of making sure user_id = nil (in the before do and in properly_close), it still ends up getting set with aasm_state == 'closed' && user_id
How is this possible? How do I figure out how it is happening?
Upvotes: 7
Views: 280
Reputation: 17968
In both cases you need to save
the update, i.e:
self.user_id = nil
self.save
Or more concisely (and skipping callbacks):
self.update_attribute :user_id, nil
Upvotes: 4