From 1b961765871eba76d50d463d3af6c3c5926f537a Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sun, 2 Dec 2012 21:24:47 +0100 Subject: backport #8403, no intermediate AR objects when eager loading. Closes #3313 Conflicts: activerecord/CHANGELOG.md activerecord/test/models/developer.rb --- activerecord/lib/active_record/relation/finder_methods.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 57ecabb537..cbb2f676c8 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -253,9 +253,11 @@ module ActiveRecord orders = relation.order_values.map { |val| val.presence }.compact values = @klass.connection.distinct("#{@klass.connection.quote_table_name table_name}.#{primary_key}", orders) - relation = relation.dup + relation = relation.dup.select(values) + + id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) + ids_array = id_rows.map {|row| row[primary_key]} - ids_array = relation.select(values).collect {|row| row[primary_key]} ids_array.empty? ? raise(ThrowResult) : table[primary_key].in(ids_array) end -- cgit v1.2.3