Stack Builders News

A collection of thoughts and notes by our team

Matt Campbell

Stack Builders Releases Valid Data Ruby Gem

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.

comments powered byDisqus

Do You Have What it Takes To Be a Stack Builder?