aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2018-11-27 12:01:59 +0200
committerBogdan Gusiev <agresso@gmail.com>2018-11-27 12:05:02 +0200
commit68acbe8a3a1d5b2fab85ba9268645f6a1ceafff2 (patch)
tree74fd7193fbfa62beddfe051701e3aad69a3d75e3 /activerecord
parent3c8259e1a982190ba19d288bb0a1ad8411587540 (diff)
downloadrails-68acbe8a3a1d5b2fab85ba9268645f6a1ceafff2.tar.gz
rails-68acbe8a3a1d5b2fab85ba9268645f6a1ceafff2.tar.bz2
rails-68acbe8a3a1d5b2fab85ba9268645f6a1ceafff2.zip
Reuse code in AR::Association#find_target
Before this patch, singular and collection associations had different implementations of the #find_target method. This patch reuses the code properly through extending the low level methods.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association.rb17
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb16
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb19
3 files changed, 23 insertions, 29 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index bf4942aac8..de7c149d1f 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -179,6 +179,23 @@ module ActiveRecord
end
private
+
+ def find_target
+ scope = self.scope
+ return scope.to_a if skip_statement_cache?(scope)
+
+ conn = klass.connection
+ sc = reflection.association_scope_cache(conn, owner) do |params|
+ as = AssociationScope.create { params.bind }
+ target_scope.merge!(as.scope(self))
+ end
+
+ binds = AssociationScope.get_bind_values(owner, reflection.chain)
+ sc.execute(binds, conn) do |record|
+ set_inverse_instance(record)
+ end
+ end
+
# The scope for this association.
#
# Note that the association_scope is merged into the target_scope only when the
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 48bb9ab066..f67e62af04 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -304,22 +304,6 @@ module ActiveRecord
private
- def find_target
- scope = self.scope
- return scope.to_a if skip_statement_cache?(scope)
-
- conn = klass.connection
- sc = reflection.association_scope_cache(conn, owner) do |params|
- as = AssociationScope.create { params.bind }
- target_scope.merge!(as.scope(self))
- end
-
- binds = AssociationScope.get_bind_values(owner, reflection.chain)
- sc.execute(binds, conn) do |record|
- set_inverse_instance(record)
- end
- end
-
# We have some records loaded from the database (persisted) and some that are
# in-memory (memory). The same record may be represented in the persisted array
# and in the memory array.
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index 8e50cce102..5395fc208b 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -31,24 +31,17 @@ module ActiveRecord
end
private
+
+ def target_scope
+ super.limit!(1)
+ end
+
def scope_for_create
super.except!(klass.primary_key)
end
def find_target
- scope = self.scope
- return scope.take if skip_statement_cache?(scope)
-
- conn = klass.connection
- sc = reflection.association_scope_cache(conn, owner) do |params|
- as = AssociationScope.create { params.bind }
- target_scope.merge!(as.scope(self)).limit(1)
- end
-
- binds = AssociationScope.get_bind_values(owner, reflection.chain)
- sc.execute(binds, conn) do |record|
- set_inverse_instance record
- end.first
+ super.first
rescue ::RangeError
nil
end