-
-
Save malachaifrazier/631bf16e1d25779bcb5f89bbefa19efe to your computer and use it in GitHub Desktop.
Find invalid records in Rails.
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
namespace :db do | |
desc "Outputs any invalid data records" | |
task :invalid_records => :environment do | |
puts "\n** Testing record validating in #{Rails.env.capitalize} environment**\n" | |
ActiveRecord::Base.send(:subclasses).each do |model| | |
puts "#{model} records (#{model.count})" | |
next if model.count == 0 | |
invalid = model.all.reject(&:valid?) | |
if invalid.size.zero? | |
puts " all valid" and next | |
else | |
puts " Some valid (#{model.count - invalid.size}), some invalid (#{invalid.size})" | |
end | |
invalid.each do |record| | |
puts " #{record.id}\tinvalid\t(#{record.errors.full_messages.to_sentence})" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment