Skip to content

Instantly share code, notes, and snippets.

@dbi
Created August 14, 2012 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbi/3349029 to your computer and use it in GitHub Desktop.
Save dbi/3349029 to your computer and use it in GitHub Desktop.
Detect invalid activerecord models in your app
# Disable logging
ActiveRecord::Base.logger = Logger.new(nil)
# Make sure all models are loaded
Dir.glob("app/models/*.rb").each do |f|
begin
require File.basename(f).sub(".rb", "")
rescue
puts "could not require #{f}"
end
end
# It's a good idea to review this list and remove models that does not need to be checked for validation errors. Especially if you have a couple of huge tables.
# subjects = ActiveRecord::Base.descendants.select {|c| c.superclass == ActiveRecord::Base}
subjects = []
data = Hash.new() { [] }
subjects.each do |klass|
klass.find_each do |p|
data[klass] << p.id unless p.valid?
end
print "#{klass.name.ljust(28)} #{data[klass].size}/#{klass.count}"
print " (#{data[klass].join(",")})" if data[klass].size > 0
puts
end;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment