Rails after_commit call back without infinite loop

How to do a after_commit for update without encountering infinite loop

class MyClass < ActiveRecord::Base
    ...
    after_commit :update_some_thing
    ..
    
    private
    
      def update_some_thing
          self.update_column(:some_time_column, Time.current)
      end
end

Since 'after_commit' will be triggered every time any db transaction is success, doing a simple 'update' will lead to an infinite loop. By doing 'update_column' we can trigger a raw SQL query to update the specific column by not invoking 'after_commit' callback or any validation check.

https://apidock.com/rails/v4.2.7/ActiveRecord/Persistence/update_columns

Last updated