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 ...


Share/Save/Bookmark

Published

Mon, 12 Mar 2007

View Comments


Want more like this?

Subscribe via RSS
or by email:

New Relic