Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

ValidationErrors 😱

Because shit happens

This gem helps you keep track of the ActiveRecord validation errors that have been triggered on a model.

It persists them on a validation_errors database table the following information:

  • time of the error
  • model name
  • model id (if available)
  • action (create/update)
  • errors.details (hash)


Validation errors happen. In some applications it might be interesting to keep track of them. This gem has been extracted from various Ruby On Rails apps where I had this need.

If you have a validation error, you want to keep track of it...but how?

  • You can use a logger, but then you have to parse the logs to get the information you need. i.e. is not structured.
  • You can use an error tracker, like Sentry, but this isn't really an error, is it? If you have many, it might pollute your errors tracker.
  • You can use a database table, and that's what this gem does.

This gem will keep track of the errors, and give you all the freedom to query it and extract statistics and make analysis. By analysing from time to time these data, you might found out the following:

  • Your UI sucks
  • Your validations are too strict
  • Your validations are too loose
  • Your client-side validations are not working
  • Your client-side validations are too loose
  • This bullet point list has been generated by copilot


Install the gem and add it to the application's Gemfile by executing:

$ bundle add validation_errors

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install validation_errors


Run bin/rails g validation_errors:install to create the migration. Stop evil spring, if the generator is not found: spring stop.

Run bin/rails db:migrate to create the tables.

The migration will create a validation_errors table. Check the migration for details. If you use Scenic it will also generate some useful views.


You can now manually track errors by calling ValidationErrors.track:


An example could be the following:

def create
    @user =
        redirect_to @user
        render :new

Yes, this will execute an additional INSERT into your database. Keep it in mind.

ValidationError.track(@model) will automatically detect if the action is create or update. You can also use a custom action name by calling ValidationError.track(@model, action: 'custom_action').


You can also track validation errors automatically by adding track_validation_errors in your model.

class User < ApplicationRecord

by doing so, validation errors are tracked automatically on each save, save!, update, or update! call on your model.


You can of course enable it on all you models by specifying it in ApplicationRecord directly:

class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true

We currently don't support (PR welcome):

  • enable globally and disable on a specific model skip_track_validation_errors
  • enable only for specific actions track_validation_errors only: [:create]
  • disable only for specific actions track_validation_errors except: [:create]
  • enable only for bang or non-bang methods track_validation_errors only_bang: true, track_validation_errors only_non_bang: true

Query your data

Now that you have installed the gem and started tracking, let's take a look at how the data are persisted and how you can query them. We store the errors in exactly the same format returned by ActiveRecord errors.details.

Given a book, that failed to save because of validation errors, you'll get the following:

id invalid_model_name invalid_model_id action details
1 Book 1 create { "base" => [{ "error" => "invalid" }], "title" => [{ "error" => "blank" }, {"error" => "invalid"}] }

The following SQL (Postgres only!) can be used to obtain a flattened view. You can use it in your queries, or create a database view:

select validation_errors.invalid_model_name,
       json_data.key as error_column,
       json_array_elements(json_data.value)->>'error' as error_type
from validation_errors, json_each(validation_errors.details) as json_data

The gem will already create this view for you, if you use Scenic.

The result is the following:

invalid_model_name invalid_model_id action error_column error_type
Book 1 create base invalid
Book 1 create title blank
Book 1 create title invalid

Let's now check some useful queries:

Count the number of errors per day

select count(*), date(created_at) 
from validation_errors 
group by date(created_at) 
order by date(created_at) desc;

Please use groupdate for more reliable results when grouping by date.


Count the number of errors per model and attribute

select validation_errors.invalid_model_name, 
       json_data.key as error_column, 
       json_array_elements(json_data.value)->>'error' as error_type, 
from validation_errors, 
     json_each(validation_errors.details) as json_data
group by 1, 2, 3
order by 4 desc

or, if you have the view above:

select invalid_model_name, error_column, count(*)
from flat_validation_errors
group by 1, 2, :error_column).count


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ValidationErrors project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.


Track ActiveRecord validation errors on your database



Code of conduct





No packages published