Index: activerecord/lib/active_record/abstract_query.rb =================================================================== --- activerecord/lib/active_record/abstract_query.rb (revision 0) +++ activerecord/lib/active_record/abstract_query.rb (revision 0) @@ -0,0 +1,43 @@ +module ActiveRecord + class AbstractRecords + attr_reader :records, :klass, :query + + delegate :connection, :instantiate, :name, :to => :klass + delegate :sql, :options, :to => :query + + def initialize(query, klass) + @query = query + @klass = klass + @loaded = false + end + + def method_missing(method_id, *args, &block) + load_records + records.send(method_id, *args, &block) + end + + def loaded? + @loaded + end + + private + + def load_records + return if @loaded + @records = connection.select_all(sql, "#{name} Load").collect! { |record| instantiate(record) } + @records.each { |record| record.readonly! } if options[:readonly] + @loaded = true + end + end + + class AbstractQuery < DelegateClass(AbstractRecords) + attr_reader :sql, :options + + def initialize(klass, sql, options = {}) + @sql = sql + @options = options + super(AbstractRecords.new(self, klass)) + end + + end +end Index: activerecord/lib/active_record/base.rb =================================================================== --- activerecord/lib/active_record/base.rb (revision 8376) +++ activerecord/lib/active_record/base.rb (working copy) @@ -1229,13 +1229,13 @@ end def find_every(options) - records = scoped?(:find, :include) || options[:include] ? - find_with_associations(options) : - find_by_sql(construct_finder_sql(options)) - - records.each { |record| record.readonly! } if options[:readonly] - - records + if scoped?(:find, :include) || options[:include] + records = find_with_associations(options) + records.each { |record| record.readonly! } if options[:readonly] + else + constructed_sql = sanitize_sql(construct_finder_sql(options)).strip + ActiveRecord::AbstractQuery.new(self, constructed_sql, options) + end end def find_from_ids(ids, options) Index: activerecord/lib/active_record.rb =================================================================== --- activerecord/lib/active_record.rb (revision 8376) +++ activerecord/lib/active_record.rb (working copy) @@ -53,6 +53,7 @@ require 'active_record/calculations' require 'active_record/serialization' require 'active_record/attribute_methods' +require 'active_record/abstract_query' ActiveRecord::Base.class_eval do extend ActiveRecord::QueryCache