Skip to content

Instantly share code, notes, and snippets.

@lukes
Last active July 18, 2022 23:43
Show Gist options
  • Save lukes/5682296 to your computer and use it in GitHub Desktop.
Save lukes/5682296 to your computer and use it in GitHub Desktop.
Find invalid records in Rails.
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