aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSantosh Wadghule <santosh.wadghule@gmail.com>2015-03-30 04:06:10 +0530
committerSantosh Wadghule <santosh.wadghule@gmail.com>2015-03-31 22:54:42 +0530
commit4596e16f1a55629ed2934abcd4fc386641d32c30 (patch)
treee5b44f0fd71a3c88d7da6487cecd94e725fbf6d8
parente2a96c071216d14659294a5a7645878656c30916 (diff)
downloadrails-4596e16f1a55629ed2934abcd4fc386641d32c30.tar.gz
rails-4596e16f1a55629ed2934abcd4fc386641d32c30.tar.bz2
rails-4596e16f1a55629ed2934abcd4fc386641d32c30.zip
Fix eager loading association using default_scope for finder methods.
- Eager loading was not working for the default_scope (class method) for 'find' & 'find_by' methods. - Fixed these by adding a new check 'respond_to?(:default_scope)'.
-rw-r--r--activerecord/lib/active_record/scoping/default.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb17
2 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/scoping/default.rb b/activerecord/lib/active_record/scoping/default.rb
index 5ec2c88b47..3590b8846e 100644
--- a/activerecord/lib/active_record/scoping/default.rb
+++ b/activerecord/lib/active_record/scoping/default.rb
@@ -35,7 +35,7 @@ module ActiveRecord
# Are there attributes associated with this scope?
def scope_attributes? # :nodoc:
- super || default_scopes.any?
+ super || default_scopes.any? || respond_to?(:default_scope)
end
def before_remove_const #:nodoc:
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 7d8b933992..0ecf2ddfd1 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -759,6 +759,23 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
+ def test_eager_with_default_scope_as_class_method_using_find_method
+ david = developers(:david)
+ developer = EagerDeveloperWithClassMethodDefaultScope.find(david.id)
+ projects = Project.order(:id).to_a
+ assert_no_queries do
+ assert_equal(projects, developer.projects)
+ end
+ end
+
+ def test_eager_with_default_scope_as_class_method_using_find_by_method
+ developer = EagerDeveloperWithClassMethodDefaultScope.find_by(name: 'David')
+ projects = Project.order(:id).to_a
+ assert_no_queries do
+ assert_equal(projects, developer.projects)
+ end
+ end
+
def test_eager_with_default_scope_as_lambda
developer = EagerDeveloperWithLambdaDefaultScope.where(:name => 'David').first
projects = Project.order(:id).to_a