Created
August 14, 2012 12:46
-
-
Save dbi/3349029 to your computer and use it in GitHub Desktop.
Detect invalid activerecord models in your app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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