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)'.
This commit is contained in:
Santosh Wadghule 2015-03-30 04:06:10 +05:30
parent e2a96c0712
commit 4596e16f1a
2 changed files with 18 additions and 1 deletions

@ -35,7 +35,7 @@ def unscoped
# 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:

@ -759,6 +759,23 @@ def test_eager_with_default_scope_as_class_method
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