Skip to content

Instantly share code, notes, and snippets.

@jamesbebbington
Created July 21, 2010 11:49
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 jamesbebbington/484378 to your computer and use it in GitHub Desktop.
Save jamesbebbington/484378 to your computer and use it in GitHub Desktop.
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