Created
September 10, 2013 14:26
-
-
Save xhoy/6510171 to your computer and use it in GitHub Desktop.
validate models vs db in an rails app
This file contains 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
# file: validate_models.rake | |
# task: rake db:validate_models | |
namespace :db do | |
desc "Run model validations on all model records in database" | |
task :validate_models => :environment do | |
puts "-- records - model --" | |
Dir.glob(Rails.root.join('app/models/**/*.rb')).each { |file| require file } | |
ActiveRecord::Base.descendants.select { |c| | |
c.base_class == c}.sort_by(&:name).each do |klass| | |
total = klass.count | |
printf "%10d - %s\n", total, klass.name | |
chunk_size = 1000 | |
(total / chunk_size + 1).times do |i| | |
chunk = klass.find(:all, :offset => (i * chunk_size), :limit => chunk_size) | |
chunk.reject(&:valid?).each do |record| | |
puts "#{record.class}: id=#{record.id}" | |
p record.errors.full_messages | |
puts | |
end rescue nil | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might be interested in
.find_each
.