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:
At the model level, you only enforce the presence of the user’s email address, like so:
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
last_name to be blank. So you add some more model validations:
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.