How to track ActiveRecord model statistics

How to track ActiveRecord model statistics

If you’re really serious about your application you have to collect and analyze its statistics. You can use Google Analytics or any other tool to track visits and basic events, or you can send specific events on demand. There’s also a way to automatically track ActiveRecord model creations and in this post I’ll show you how easy it is.

The solution

Let’s dig into the most important source code:

# config/initializers/creation_listener.rb
module CreationListener
  def inherited(subclass)
    class_name =
    subclass.after_commit :on => :create do "[#{}] Model created: '#{class_name}'"


I think you already know what it does - it binds to ActiveRecord::Base’s callback and puts appropriate message with time of creation and class name of created model. Then log messages are parsed with the following rake task:

# lib/tasks/creations.rake
task creations: :environment do
  creation_entry_regexp = /\[([\w\W]+)\] Model created: '([\w\W]+)'/
  log_path = File.join(Rails.root, "log", "development.log")
  date_to_calculate =

  result ={|hash, key| hash[key] = 0}, "r") do |f|
    f.each_line do |line|
      if line =~ creation_entry_regexp
        creation_time = Date.parse($1)
        model_name = $2.strip
        if creation_time == date_to_calculate
          result[model_name] += 1

  puts "Statistics for: #{date_to_calculate}"
  result.each_pair do |key, value|
    puts "  #{key}: #{value}"

I just define how to look for and parse creation messages, which log file I want to check and for which date. Then both parsing and calculating result happens - if line matches to regexp and given date is one we are looking for it increments result for given model. So as a result you get the list of all model classes which instances were created on given day.

You can check how it works using this sample project.

Logger? Seriously?!

In this example I assume, that the only method to persist information about created model is to use log messages. Of course it’s just a simplification. In real world you don’t want to gather all statistics in log: it can be time consuming to calculate the results, logs can be really big or rotated.

For alternative persistence method you have to be aware of 2 things:

  1. It shouldn’t slow down response time too much.
  2. It should be threadsafe.

If you dig into chillout gem you’ll see how you can achieve that - you can use Thread.current to pass information about created models and middleware to get this information and send it to the storage - in our case to API endpoint. There are a few simple optimizations that will help you not to kill app’s performance when dealing with API, but that’s subject for another post.

Struggling to apply DDD concepts to your Rails app?

For a few years we’ve been studying Domain-Driven Design and applying its techniques in our projects. In this book we describe techniques that you can use in new and old Rails apps to achieve better architecture. They were useful to us and we are sure they are going to be useful for you.

Click here to read more!

Domain-Driven Rails

There is more... check out other books published by us

You might also like