Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Last active August 29, 2015 14:19
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 IanVaughan/3e8868a498b5b92c1f14 to your computer and use it in GitHub Desktop.
Save IanVaughan/3e8868a498b5b92c1f14 to your computer and use it in GitHub Desktop.
Finds invalid rails records
#!/usr/bin/env ruby
puts "Loading Rails..."
require 'ruby-progressbar'
require './config/environment'
require "csv"
puts "Booting Rails..."
Rails.application.eager_load!
puts "Getting Models..."
models = ActiveRecord::Base.descendants
puts "Analysing..."
CSV.open("results.csv", "w") do |csv|
models_with_index.each do |model, index|
bar = ProgressBar.create(title: "#{model} (#{index}/#{models.count})", total: model.count)
model.find_in_batches do |batch|
batch.each_with_index do |record, index|
bar.increment
if !record.valid? && !record.valid?
record.errors.messages.each do |message|
csv << [model.to_s, message, record.id, record.created_at.to_s(:basic), record.updated_at.to_s(:basic)]
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment