Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

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
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.