Home > Mobile >  Is it possible to use callback conditionals in an observer?
Is it possible to use callback conditionals in an observer?

Time:09-16

I have a model callback like this:

after_save :foobar, except: :create

The code this runs is mostly irrelevant to the model itself, so I wish to move it into an observer. I find I can do something like this:

def after_save(model)
  foobar(model)
end

However, this does not maintain the except: :create behavior of my callback, which is very important in this instance.

Is it possible to use callback conditionals such as except in a Rails observer? If so, how would I go about doing so?

CodePudding user response:

when you define

after_save :foobar, except: :create

it means that you only trigger the callback for update, so just define in your observer:

def after_update(model)
  # do something here
end
  • Related