Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
desc <<-EOT
Display the id and error messages of all invalid instances of each model in
model_classes, which should be a comma separated list of model names. If
model_classes is blank all models will be reported.
EOT
task :validate_models, [:models] => :environment do |t, args|
model_classes = if args[:models]
args[:models].split(',').map(&:constantize)
else
Rails.application.eager_load!
ActiveRecord::Base.descendants
end
model_classes.each do |model_class|
$stderr.puts "# #{model_class.name}"
model_class.all.reject(&:valid?).each do |model|
puts "%s:%d:%s" % [model_class.name, model.id, model.errors.full_messages]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment