Want to take your software engineering career to the next level? Join the mailing list for career tips & advice Click here


Attach comments to ActiveRecord's SQL queries

Subscribe to updates I use marginalia

Statistics on marginalia

Number of watchers on Github 5
Number of open issues 0
Main language Ruby
Open pull requests 0+
Closed pull requests 0+
Last commit about 7 years ago
Repo Created over 6 years ago
Repo Last Updated over 2 years ago
Size 124 KB
Organization / Authorgithub
Page Updated
Do you use marginalia? Leave a review!
View on github
Fresh, new opensource launches 🚀🚀🚀
Software engineers: It's time to get promoted. Starting NOW! Subscribe to my mailing list and I will equip you with tools, tips and actionable advice to grow in your career.
Evaluating marginalia for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

marginalia Build Status

Attach comments to your ActiveRecord queries. By default, it adds the application, controller, and action names as a comment at the end of each query.

This helps when searching log files for queries, and seeing where slow queries came from.

For example, once enabled, your logs will look like:

Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` 
WHERE `accounts`.`queenbee_id` = 1234567890 

You can also use these query comments along with a tool like pt-query-digest to automate identification of controllers and actions that are hotspots forslow queries.

This gem was created at 37signals. You can read more about how we use it on our blog.

This has been tested and used in production with both the mysql and mysql2 gems, tested on Rails 2.3.5 through 3.2-stable. It has also been tested for sqlite3 and postgres.

Patches are welcome for other database adapters.

The preferred way to get support is to send an email to marginalia@librelist.com. Github issues and pull requests will be checked occassionally, but email is the fastest way to get help.


For Rails 3.x:

# Gemfile
gem 'marginalia'

require 'marginalia/railtie'

For Rails 2.x:

If using cached externals, add to your config/externals.yml file.

Or, if your prefer using config.gem, you can use:

config.gem 'marginalia'

Finally, if bundled, you'll need to manually run the initialization step in an initializer, e.g.:

# Gemfile
gem 'marginalia', :require => false

require 'marginalia'


Optionally, you can set the application name shown in the log like so in an initializer (e.g. config/initializers/marginalia.rb):

Marginalia.application_name = "BCX"

For Rails 3 applications, the name will default to your Rails application name. For Rails 2 applications, rails is used as the default application name.

You can also configure the components of the comment that will be appended, by setting Marginalia::Comment.components. By default, this is set to:

Marginalia::Comment.components = [:application, :controller, :action]

Which results in a comment of application:#{application_name},controller:#{controller.name},action:#{action_name}.

You can re-order or remove these components. You can also add additional comment components of your desire by defining new module methods for Marginalia::Comment which return a string. For example:

module Marginalia
  module Comment
    def self.mycommentcomponent

Marginalia::Comment.components = [:application, :mycommentcomponent]

Which will result in a comment like application:#{application_name},mycommentcomponent:TEST The calling controller is available to these methods via @controller.

Marginalia ships with :application, :controller, and :action enabled by default. In addition, implementation is provided for:

  • :line (for file and line number calling query). :line supports a configuration by setting a regexp in Marginalia::Comment.lines_to_ignore to exclude parts of the stacktrace from inclusion in the line comment.

Pull requests for other included comment components are welcome.


Start by bundling and creating the test database:

rake db:create

Then, running rake will run the tests on both the mysql and mysql2 adapters:

marginalia questions on Stackoverflow (View All Questions)
  • How to create marginalia in paged media?
  • Marginalia / Docco / etc for Java Maven projects?
  • HTML5 markup for content-related marginalia
  • Clojure: Marginalia: Multi Page Documentation
marginalia list of languages used
Other projects in Ruby