Today I Learned

This project exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as selected friends of JetRockets. We hope you enjoy learning along with us.

Use gem Recorder for db log πŸ’ͺ

Gem Recorder tracks changes of your Rails models

For start use this library in Rails 5.2 you need add gem to the Gemfile:

gem 'recorder', github: 'jetrockets/recorder', branch: 'rails_5_2'


> bundle install
> rails g recorder:install
> rails db:migrate

And final step β€” add to model:

include ::Recorder::Observer
recorder async: false

And it’s all πŸŽ‰

For an example - logs can be issued in the representer:

Recorder::Revision.where(item: object).order(id: :DESC).limit(10).map do |revision|
    userId: revision.user_id,
    userEmail: revision.user&.email,
    change: changes_type(revision)

def changes_type(revision)
  if['changes'].include?('drive_file_id') &&['changes'].size > 4 &&['changes']['drive_file_id'].present?
    'Object updated & Document generated'
  elsif['changes'].include?('drive_file_id') &&['changes'].size == 4
    'Document generated'
  elsif['changes'].include?('number') &&['changes']['number'].nil?
    'Object created'
    'Object updated'

But of course this is an abstract example with my data 😏

Looking for help? JetRockets has been an leader in Ruby on Rails since 2012. Rails is a core skill for each developer at JetRockets and we'd love to take a look at your project. Contact us and find out how we can help you.