Your form has successfully submitted

One of our teammates will get back to you soon.

Stack Builders Releases Valid Data Ruby Gem

We've released one of our first open-source Ruby gems: it's called valid_data, and you can use it to scan your ActiveRecord models for invalid rows. We use the gem at Stack Builders from time-to-time in order to "check in" with our datastore and make sure we don't have validation-related problems in our applications.

valid_data is a simple, lightweight reporting gem (see the source code or download it from RubyGems). By exporting a Rake task (rake validate_records) to your Rails application, you gain the ability to introspect your ActiveRecord (table-backed) models from the console - specifically, it's checking for rows in your database that Rails would instantiate to invalid model instances. Here's a quick example:

You have a User model, and the schema looks like this:

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "first_name",
    t.string   "last_name",
    t.datetime "created_at"
    t.datetime "updated_at"

At the model level, you only enforce the presence of the user's email address, like so:

class User < ActiveRecord::Base
  validates_presence_of :email

Great! Even better, your application now has 10,000 users signed up. Right about now, you realize it was probably a bad idea to allow a user's first_name and/or last_name to be blank. So you add some more model validations:

class User < ActiveRecord::Base
  validates_presence_of :email,

Immediately, a good chunk of those 10,000 new users you just signed up (i.e. any that didn't provide first- or last-name data) are invalid according to your User model logic.

This is an extreme example, but the point is that changes in application-level validations happen all the time across your Rails models. valid_data was designed to make sure you're at least aware of the potential problem looming in your datastore as a result of these changing validations.

Published on: May. 21, 2014

Written by:

Software Developer

Matt Campbell

Subscribe to our blog

Join our community and get the latest articles, tips, and insights delivered straight to your inbox. Don’t miss it – subscribe now and be part of the conversation!

We care about your data. Check out our Privacy Policy.