public

Rails Lighthouse ticket #6164

  • Download Gist
remove_extra_select_when_associ.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
From 1c3650832d03c3e96e46ffa777da786ef9e7fa1b Mon Sep 17 00:00:00 2001
From: Joseph Palermo <pair+jpalermo@pivotallabs.com>
Date: Wed, 16 Mar 2011 23:29:46 +0800
Subject: [PATCH] Remove extra select table.* when association class has default_scope with a select
 
---
.../associations/preloader/association.rb | 6 +++++-
.../preloader/has_and_belongs_to_many.rb | 4 ++++
activerecord/test/cases/relation_scoping_test.rb | 5 +++++
activerecord/test/models/person.rb | 1 +
activerecord/test/models/reference.rb | 5 +++++
5 files changed, 20 insertions(+), 1 deletions(-)
 
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 7256dd5..e2b040d 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -97,7 +97,7 @@ module ActiveRecord
scope = scope.where(process_conditions(options[:conditions]))
scope = scope.where(process_conditions(preload_options[:conditions]))
- scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star])
+ scope = scope.select(select_scope_values)
scope = scope.includes(preload_options[:include] || options[:include])
if options[:as]
@@ -111,6 +111,10 @@ module ActiveRecord
scope
end
+ def select_scope_values
+ preload_options[:select] || options[:select]
+ end
+
def process_conditions(conditions)
if conditions.respond_to?(:to_proc)
conditions = klass.send(:instance_eval, &conditions)
diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
index 24be279..f203018 100644
--- a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
+++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
@@ -44,6 +44,10 @@ module ActiveRecord
super.joins(join).select(join_select)
end
+ def select_scope_values
+ super || table[Arel.star]
+ end
+
def join_select
association_key.as(Arel.sql(association_key_name))
end
diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb
index cda2850..f9e5b4d 100644
--- a/activerecord/test/cases/relation_scoping_test.rb
+++ b/activerecord/test/cases/relation_scoping_test.rb
@@ -268,6 +268,11 @@ class HasManyScopingTest< ActiveRecord::TestCase
magician = BadReference.find(1)
assert_equal [magician], michael.bad_references
end
+
+ def test_should_use_default_select_scope_on_associations
+ michael = Person.where(:id => people(:michael).id).includes(:favouriteless_references).first
+ assert_equal false, michael.favouriteless_references.first.attributes.has_key?('favourite')
+ end
end
class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index cc3a4f5..c859e5b 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -7,6 +7,7 @@ class Person < ActiveRecord::Base
has_many :references
has_many :bad_references
+ has_many :favouriteless_references
has_many :fixed_bad_references, :conditions => { :favourite => true }, :class_name => 'BadReference'
has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
diff --git a/activerecord/test/models/reference.rb b/activerecord/test/models/reference.rb
index 06c4f79..22f0c4d 100644
--- a/activerecord/test/models/reference.rb
+++ b/activerecord/test/models/reference.rb
@@ -19,3 +19,8 @@ class BadReference < ActiveRecord::Base
self.table_name ='references'
default_scope :conditions => {:favourite => false }
end
+
+class FavouritelessReference < ActiveRecord::Base
+ self.table_name = 'references'
+ default_scope select(column_names - ['favourite'])
+end
--
1.7.3

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.