Today I Learned

hashrocket A Hashrocket project

Check if in transaction in rails

ActiveRecord::ConnectionAdapters::DatabaseStatements can list open transactions, if there are none, chances are you're not in a transaction.

class WriteALot < ApplicationRecord
  def self.vacuum
    if connection.open_transactions.blank?
      connection.execute("vacuum verbose analyze #{table_name}")
    end
  end
end
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.