Execute after_save callback only if an attribute is modified

for instance if I want to fetch a url if the url attribute has
changed.

I know that I can use before_save and do:

def get_url
  get_url if url_changed?
end

However, this is not really correct. If that call is using delayed job
or some other technique since the object is not yet saved in the
before_save callback, for instance.

def get_url
  Queue.add(:fetch_url, id)
end

might end up getting the old URL if it is executed before the update
completes. The prior technique would also end up using the new URL if
the update fails. This is the sort of thing that should be done after
the object is updated.

I'd like to do something like:

after_save :get_url, :if => url_changed?

but this won't work because the url is no longer "dirty" after the
save.

Settings flags in the before_save and executing after_save gets ugly
real fast, and has potential for infinite loops, requiring hackery.

Is there a clean way to do this?

Have you tried this? Unless something's broken, the dirty attributes
shouldn't be cleared until *after* all the callbacks run.

--Matt Jones

My bad. You are correct. I'm not sure why I thought otherwise, but I
did a simple test and the flags are indeed not cleared until after all
the callbacks are run. Sorry for the confusion - must have been a
different bug.