Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module ActiveRecordExtensions
module ClassExtensions
# Returns +Array+ of all +ActiveRecord+ model classes
def models
Dir.glob(Rails.root.join('app', 'models', '**', '*rb')).each do |model|
ActiveSupport::Dependencies::Loadable.require_or_load model
end
Object.subclasses_of ActiveRecord::Base
end
# Returns +Array+ of all invalid records.
# Useful for sanity checking in migrations.
def invalid_records
returning [] do |invalids|
(self == ActiveRecord::Base ? models : [self]).each do |model|
if model.table_exists?
model.find_in_batches(:batch_size => 100) do |records|
records.each { |record| invalids << record unless record.valid? }
end
end
end
end
end
end
end
@jamesbebbington
Copy link
Author

Thought I'd throw this out to the Internets, someone might find it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment