Created
July 21, 2010 11:49
-
-
Save jamesbebbington/484378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thought I'd throw this out to the Internets, someone might find it useful.