15

It looks like Paperclip doesn't honor the ActiveRecord dirty model. How do I detect the change in after_save callback.

class User

  has_attachment :avatar    
  after_save :do_something

  def do_something
    if name_changed?
      #
    end

    # How to determine avatar was changed?
    #if avatar_changed?
    #  #
    #end

  end
end

Note

I know I can detect the change in before_save callback using avatar.dirty? call, but the dirty flag is set to false after save.

I can add a processor, but I need to perform my actions after the model data is saved.

Harish Shetty
  • 64,083
  • 21
  • 152
  • 198

2 Answers2

29

You could try accessing the _changed? method for one of the attributes:

if avatar_updated_at_changed?
  # do something
end
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
5

When I need access to this data after save, I typically take this approach:

class Foo

  has_attachment :avatar
  before_save :check_for_avatar_changes
  after_save :do_something

  def do_something
    if @avatar_has_changes
      #
    end
  end

  def check_for_avatar_changes
    @avatar_has_changes = self.avatar.dirty?
  end

end
josht
  • 51
  • 1
  • 1
  • To improve the quality of your post please include how/why your post will solve the problem. – Mick MacCallum Oct 06 '12 at 06:48
  • There's a problem in the code of check_for_avatar_changes... If the funtion returns false, the model won't get saved, because it's a before_save callback. – Christian Sep 21 '16 at 14:50