ActiveRecordでクエリ発行元をコメントに含める
code:ruby
# frozen_string_literal: true
dowsing = Module.new do
def execute(sql, *)
source_location = query_source_location(caller.lazy)
super
end
private
def query_source_location(locations)
source_location = ActiveRecord::LogSubscriber.backtrace_cleaner.clean(locations).first
source_location&.remove!('*')
source_location
end
end
ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(dowsing)
end