How can I update attributes in after_save without causing a recursion in rails 2.3?

Any update_attribute in an after_save callback will cause recursion, in Rails3+. What should be done is:

after_save :updater!
# Awesome Ruby code
# ...
# ...

private

  def updater!
    self.update_column(:column_name, new_value) # This will skip validation gracefully.
  end

Here is some documentation about it: https://guides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks


You could wrap it in a conditional, something like:

def save_thumbnail
  if File.basename(thumb) != thumbnail_file_name
    self.update_attributes(
      :thumbnail_file_name => File.basename(thumb), 
      :thumbnail_content_type => 'image/jpeg'
    )
  end
end

That way it would only run once.


Rails 2:

Model.send(:create_without_callbacks)
Model.send(:update_without_callbacks)

Rails 3:

Vote.skip_callback(:save, :after, :add_points_to_user)

See this question:

How to skip ActiveRecord callbacks?