From da6fa77a721e897658a8962fa1829ae53b47f78f Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 28 Jun 2010 16:37:38 -0400 Subject: [PATCH] fixes #2362, eager loading :through associations will join the :source model if there are :conditions --- .../lib/active_record/association_preload.rb | 7 ++++++- activerecord/test/cases/associations/eager_test.rb | 6 ++++++ activerecord/test/models/post.rb | 1 + 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 f13c250..cbec578 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 445e688..40859d4 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 dd06822..6c7b93b 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 -- 1.7.0.4