mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
pass the association and connection to the scope method
This commit is contained in:
parent
3b675f05ff
commit
8e6ef92fd9
3 changed files with 7 additions and 11 deletions
|
@ -94,7 +94,7 @@ module ActiveRecord
|
||||||
# actually gets built.
|
# actually gets built.
|
||||||
def association_scope
|
def association_scope
|
||||||
if klass
|
if klass
|
||||||
@association_scope ||= AssociationScope.new(self).scope
|
@association_scope ||= AssociationScope.new.scope(self, klass.connection)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,12 @@
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
module Associations
|
module Associations
|
||||||
class AssociationScope #:nodoc:
|
class AssociationScope #:nodoc:
|
||||||
attr_reader :association
|
def scope(association, connection)
|
||||||
|
|
||||||
def initialize(association)
|
|
||||||
@association = association
|
|
||||||
end
|
|
||||||
|
|
||||||
def scope
|
|
||||||
klass = association.klass
|
klass = association.klass
|
||||||
reflection = association.reflection
|
reflection = association.reflection
|
||||||
scope = klass.unscoped
|
scope = klass.unscoped
|
||||||
owner = association.owner
|
owner = association.owner
|
||||||
alias_tracker = AliasTracker.new klass.connection
|
alias_tracker = AliasTracker.new connection
|
||||||
|
|
||||||
scope.extending! Array(reflection.options[:extend])
|
scope.extending! Array(reflection.options[:extend])
|
||||||
add_constraints(scope, owner, klass, reflection, alias_tracker)
|
add_constraints(scope, owner, klass, reflection, alias_tracker)
|
||||||
|
|
|
@ -6,8 +6,10 @@ module ActiveRecord
|
||||||
module Associations
|
module Associations
|
||||||
class AssociationScopeTest < ActiveRecord::TestCase
|
class AssociationScopeTest < ActiveRecord::TestCase
|
||||||
test 'does not duplicate conditions' do
|
test 'does not duplicate conditions' do
|
||||||
association_scope = AssociationScope.new(Author.new.association(:welcome_posts))
|
association_scope = AssociationScope.new
|
||||||
wheres = association_scope.scope.where_values.map(&:right)
|
scope = association_scope.scope(Author.new.association(:welcome_posts),
|
||||||
|
Author.connection)
|
||||||
|
wheres = scope.where_values.map(&:right)
|
||||||
assert_equal wheres.uniq, wheres
|
assert_equal wheres.uniq, wheres
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue