overstimulate

Report of invalid records (ActiveRecord)

While working on bringing userscripts.org into the 21st century, we've added validations that our models were missing.

Unfortunately this has the side effect of making previously valid records invalid. (email addresses with and @ sign, missing names, ...)

To help resolve those issues I built a small rake task, which iterates through each record, testing if it is valid, if not adding the id to a list.

def invalids(model, verbose = false)
  errors = {}
  records = model.find(:all)
  records.each_with_index do |record, i|
    next if record.valid?
    record.errors.full_messages.each do |msg|
      errors[msg] ||= []
      errors[msg] << record.id
    end
    if verbose
        print "#{i} of #{records.length} | "
        print (errors.keys.collect { |k| "#{k}: #{errors[k].length}" }).join(', ')
        print "\n"
    end
  end
  errors
end

After running this overnight, I know what to fix on our production user table.

Email has already been taken: 54
Display name has already been taken: 3066
Email can't be blank: 4
Display name can't be blank: 107

And I have a list of the ids for each issue. Once I get these fixed I can move on to more exciting things like fixing tagging, versioning, and ...


Responses to "Report of invalid records (ActiveRecord)"

  1. Wed, 26 Dec 2007 Mihai says:
    Very nice article. Thanks for the info
  2. Mon, 25 Feb 2008 Webmaster Marketplace says:
    Work Files are customized lists of patent records you create to work with in any manner you choose. Work Files can be saved for an indefinite period of time. When creating Work Files, you can combine the results from multiple searches into a consolidated list, choose just a few patents, enter multiple patent numbers, or import a list of patents from another application. Work Files can contain up to 20,000 records.

Leave a response

My Card Add to your Address Book

Jesse Andrews
open source, web browsers, web services, web sites & folk dancing. contacts/sites

Keep Up To Date

Get updates via RSS or
get email when I blog

Previous Blog Posts