Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mattvague
Last active August 29, 2015 14:01
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 mattvague/760588b9d0f9fa356255 to your computer and use it in GitHub Desktop.
Save mattvague/760588b9d0f9fa356255 to your computer and use it in GitHub Desktop.
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