Is it possible to pass any context to an `after_update` callback?

I have a special operation in my model that I don’t want a specific after_update callback to run only in that situation. Is it possible?

Sample code

class User
  after_update :foo, unless: -> { validation_context == :special }

  def special_operation
    save(context: :special)
  end

  def foo
    puts "some code"
  end
end

What I got

foo ran because validation_context is nil.

What I want

foo does not run.

Is it even possible?

Not with validation as the the validation context is only for validations :slight_smile: You could achieve the same thing with an ivar

class User
  attr_reader :skip_foo
  after_update :foo, unless: :skip_foo

  def special_operation
    @skip_foo = true
    save
  ensure
    @skip_foo = nil
  end

  def foo
    puts "some code"
  end
end
1 Like