aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <grant@pipelinedealsco.com>2010-06-28 16:37:38 -0400
committerJosé Valim <jose.valim@gmail.com>2010-07-08 22:36:26 +0200
commit17650e394fae26984d506fd0f705bc32e5a5de27 (patch)
tree10f2628b9b4db4a4a0df3dc8c5f061d2aac5d8e1
parente848ab527ca9da1a6cf2a8485163f01daf3f66d1 (diff)
downloadrails-17650e394fae26984d506fd0f705bc32e5a5de27.tar.gz
rails-17650e394fae26984d506fd0f705bc32e5a5de27.tar.bz2
rails-17650e394fae26984d506fd0f705bc32e5a5de27.zip
Eager loading :through associations will join the :source model if there are :conditions. [#2362 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
-rw-r--r--activerecord/lib/active_record/association_preload.rb7
-rw-r--r--activerecord/test/cases/associations/eager_test.rb6
-rw-r--r--activerecord/test/models/post.rb1
3 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index f13c250ca4..cbec5789fd 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -282,7 +282,12 @@ module ActiveRecord
end
end
else
- records.first.class.preload_associations(records, through_association)
+ options = {}
+ options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions]
+ options[:order] = reflection.options[:order]
+ options[:conditions] = reflection.options[:conditions]
+ records.first.class.preload_associations(records, through_association, options)
+
records.each do |record|
through_records.concat Array.wrap(record.send(through_association))
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 445e6889c0..40859d425f 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -357,6 +357,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
+ def test_eager_with_has_many_through_with_conditions_join_model_with_include
+ post_tags = Post.find(posts(:welcome).id).misc_tags
+ eager_post_tags = Post.find(1, :include => :misc_tags).misc_tags
+ assert_equal post_tags, eager_post_tags
+ end
+
def test_eager_with_has_many_and_limit
posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
assert_equal 2, posts.size
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index dd06822cfd..6c7b93be87 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -58,6 +58,7 @@ class Post < ActiveRecord::Base
end
end
+ has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => "tags.name = 'Misc'"
has_many :funky_tags, :through => :taggings, :source => :tag
has_many :super_tags, :through => :taggings
has_one :tagging, :as => :taggable