aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record.rb
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/lib/active_record.rb
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/lib/active_record.rb')
-rw-r--r--activerecord/lib/active_record.rb1
1 files changed, 1 insertions, 0 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