diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-11-28 01:07:53 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-11-28 01:09:35 +0900 |
commit | 93c94973ab574083a8cd1868aae03993ca156c34 (patch) | |
tree | 3b08048d058e0f12edae10a1035c265a2b82aca6 /activerecord/lib/active_record/associations | |
parent | f2ab8b64d4d46d7199f94c3e21021f414a4d259a (diff) | |
download | rails-93c94973ab574083a8cd1868aae03993ca156c34.tar.gz rails-93c94973ab574083a8cd1868aae03993ca156c34.tar.bz2 rails-93c94973ab574083a8cd1868aae03993ca156c34.zip |
Revert "Merge pull request #34538 from bogdan/reuse-find-target"
This reverts commit f2ab8b64d4d46d7199f94c3e21021f414a4d259a, reversing
changes made to b9c7305dbe57931a153a540d49ae5d469af61a14.
Reason: `scope.take` is not the same with `scope.to_a.first`.
Diffstat (limited to 'activerecord/lib/active_record/associations')
3 files changed, 28 insertions, 23 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index de7c149d1f..bf4942aac8 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -179,23 +179,6 @@ 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 f67e62af04..c4741c9fe6 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -303,6 +303,21 @@ 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 # 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 diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 5395fc208b..8e50cce102 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -31,17 +31,24 @@ module ActiveRecord end private - - def target_scope - super.limit!(1) - end - def scope_for_create super.except!(klass.primary_key) end def find_target - super.first + 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 rescue ::RangeError nil end |