def count_sql_creates counts = Hash.new(0) logger = -> (event, start, finish, id, payload) do if (table = payload[:sql][/INSERT INTO "(.+?)"/, 1]) counts[table] += 1 end end subscription = ActiveSupport::Notifications.subscribe('sql.active_record', logger) yield ActiveSupport::Notifications.unsubscribe(subscription) counts end count_sql_creates { ... some code that creates records ... } # => { companies: 2, orders: 3, ... }