aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2013-04-25 21:37:44 +0100
committerJon Leighton <j@jonathanleighton.com>2013-05-10 10:36:26 +0200
commitd7abe91cc73a8991033042f4cb7467bba7fa2339 (patch)
treefd2c944aeb46862bf5c98b4ae389b83bf6e93171 /activerecord
parent0593c00dfbdab221116e49d3b0a7c57605160fe2 (diff)
downloadrails-d7abe91cc73a8991033042f4cb7467bba7fa2339.tar.gz
rails-d7abe91cc73a8991033042f4cb7467bba7fa2339.tar.bz2
rails-d7abe91cc73a8991033042f4cb7467bba7fa2339.zip
Set the inverse when association queries are refined
Suppose Man has_many interests, and inverse_of is used. Man.first.interests.first.man will correctly execute two queries, avoiding the need for a third query when Interest#man is called. This is because CollectionAssociation#first calls set_inverse_instance. However Man.first.interests.where("1=1").first.man will execute three queries, even though this is obviously a subset of the records in the association. This is because calling where("1=1") spawns a new Relation object from the CollectionProxy object, and the Relation has no knowledge of the association, so it cannot set the inverse instance. This commit solves the problem by making relations spawned from CollectionProxies return a new Relation subclass called AssociationRelation, which does know about associations. Records loaded from this class will get the inverse instance set properly. Fixes #5717. Live commit from La Conf! :sparkles:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record.rb1
-rw-r--r--activerecord/lib/active_record/association_relation.rb14
-rw-r--r--activerecord/lib/active_record/associations/association.rb6
-rw-r--r--activerecord/test/cases/associations_test.rb13
4 files changed, 33 insertions, 1 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index 0330c0f37f..994cacb4f9 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -76,6 +76,7 @@ module ActiveRecord
autoload :AutosaveAssociation
autoload :Relation
+ autoload :AssociationRelation
autoload :NullRelation
autoload_under 'relation' do
diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb
new file mode 100644
index 0000000000..e2976577f6
--- /dev/null
+++ b/activerecord/lib/active_record/association_relation.rb
@@ -0,0 +1,14 @@
+module ActiveRecord
+ class AssociationRelation < Relation
+ def initialize(klass, table, association)
+ super(klass, table)
+ @association = association
+ end
+
+ private
+
+ def exec_queries
+ super.each { |r| @association.set_inverse_instance r }
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 710babe024..608a6af16c 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -122,7 +122,11 @@ module ActiveRecord
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope)
def target_scope
- klass.all
+ all = klass.all
+ scope = AssociationRelation.new(klass, klass.arel_table, self)
+ scope.merge! all
+ scope.default_scoped = all.default_scoped?
+ scope
end
# Loads the \target if needed and returns it.
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 95c571fd03..0f2d22a4a2 100644
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -18,6 +18,8 @@ require 'models/ship'
require 'models/liquid'
require 'models/molecule'
require 'models/electron'
+require 'models/man'
+require 'models/interest'
class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
@@ -242,6 +244,17 @@ class AssociationProxyTest < ActiveRecord::TestCase
david = developers(:david)
assert david.projects.equal?(david.projects)
end
+
+ test "inverses get set of subsets of the association" do
+ man = Man.create
+ man.interests.create
+
+ man = Man.find(man.id)
+
+ assert_queries(1) do
+ assert_equal man, man.interests.where("1=1").first.man
+ end
+ end
end
class OverridingAssociationsTest < ActiveRecord::TestCase