Skip to content

Instantly share code, notes, and snippets.

@hartct
Created May 13, 2013 06:11
Show Gist options
  • Save hartct/5566449 to your computer and use it in GitHub Desktop.
Save hartct/5566449 to your computer and use it in GitHub Desktop.
namespace :validate do
desc "Validates all model records"
task :models => :environment do
Dir.glob(Rails.root.to_s + '/app/models/*.rb').each { |file| require file }
objects = ObjectSpace.each_object(Class) do |klass|
if ActiveRecord::Base > klass
begin
all_records = klass.all
rescue
puts "Error querying model #{klass} - maybe the table doesn't exist?"
next
end
all_records.each do |r|
if !r.valid?
puts "Invalid record in model #{klass} with id #{r.id}!"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment