Last active
August 29, 2015 14:01
-
-
Save mattvague/760588b9d0f9fa356255 to your computer and use it in GitHub Desktop.
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 :invalid_records do | |
task check: [:environment] do | |
puts "Checking for invalid records...\n" | |
invalid_records = [] | |
total_record_count = 0 | |
# Make sure that all AR models are required | |
Dir["#{Rails.root}/app/models/**/*.rb"].each { |file_path| require file_path rescue nil } | |
# Iterate through all records and mark invalid ones | |
ActiveRecord::Base.descendants.select(&:descends_from_active_record?).sort_by(&:name).each do |klass| | |
klass.find_each do |record| | |
total_record_count += 1 | |
invalid_records << record unless record.valid? | |
end | |
end | |
# Output invalid record results | |
if invalid_records.any? | |
puts %Q{ | |
Found #{invalid_records.count} of #{total_record_count} invalid records | |
--------------------} | |
invalid_records.each do |invalid_record| | |
puts %Q{ | |
#{invalid_record.class.to_s} ##{invalid_record.id} | |
Errors: #{invalid_record.errors.full_messages.uniq.to_sentence}} | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment