A long-lived project that still receives updates
Comments for activerecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Adds comments to your activerecord queries so you can seem where they came from or what user caused them.

ActiveRecord 6.0 introduces an API to annotate queries, so use that if you can, but it doesn't provide a way to pass a block.

Install

gem install active_record-comments

Usage

require "active_record/comments"

# => SELECT ... /* user.rb:123 */
result = ActiveRecord::Comments.comment("user.rb:123") { User.where("x like y").count }

# => SELECT ... /* account cleanup initial */
result = ActiveRecord::Comments.comment("account cleanup") do
  ActiveRecord::Comments.comment("initial") { User.where("x like y").count }
end

If you're using raw SQL rather than Active Record to make your query, you will need to use exec_query rather than execute for comments to be added. e.g.

require 'active_record/comments'

ActiveRecord::Comments.comment("My comment")

sql_query = "SELECT * FROM users;"
result = ActiveRecord::Base.connection.exec_query(sql_query)

# => SELECT * FROM users /* My comment */

If you're replacing execute with exec_query to get comments, exec_query.rows returns data in the same format as execute.entries.

Author

Michael Grosser
michael@grosser.it
License: MIT