Skip to content

Commit

Permalink
Avoid duplicated set_inverse_instance for target scope
Browse files Browse the repository at this point in the history
Because `scope` (`target_scope`) is a `AssociationRelation`.
`AssociationRelation` handles `set_inverse_instance`.

https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/association_relation.rb#L31-L33

See also #26022.
  • Loading branch information
kamipo committed Aug 3, 2016
1 parent d46d61e commit 954b197
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
Expand Up @@ -455,23 +455,20 @@ def null_scope?
end

private
def get_records
return scope.to_a if skip_statement_cache?

conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)
}
end

binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end

def find_target
records = get_records
return scope.to_a if skip_statement_cache?

conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)
}
end

binds = AssociationScope.get_bind_values(owner, reflection.chain)
records = sc.execute(binds, klass, conn)
records.each { |record| set_inverse_instance(record) }
records
end
Expand Down
Expand Up @@ -196,7 +196,7 @@ def delete_through_records(records)

def find_target
return [] unless target_reflection_has_associated_record?
get_records
super
end

# NOTE - not sure that we can actually cope with inverses here
Expand Down
Expand Up @@ -44,8 +44,8 @@ def create_scope
scope.scope_for_create.stringify_keys.except(klass.primary_key)
end

def get_records
return scope.limit(1).records if skip_statement_cache?
def find_target
return scope.take if skip_statement_cache?

conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
Expand All @@ -56,11 +56,7 @@ def get_records
end

binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end

def find_target
if record = get_records.first
if record = sc.execute(binds, klass, conn).first
set_inverse_instance record
end
end
Expand Down

0 comments on commit 954b197

Please sign in to comment.