Skip to content

Commit

Permalink
fixes #2362, eager loading :through associations will join the :sourc…
Browse files Browse the repository at this point in the history
…e model if there are :conditions

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Grant Ammons authored and josevalim committed Jul 8, 2010
1 parent 2d3bc99 commit 0963774
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/association_preload.rb
Expand Up @@ -282,7 +282,11 @@ def preload_through_records(records, reflection, through_association)
end
through_records.flatten!
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)
through_records = records.map {|record| record.send(through_association)}.flatten
end
through_records.compact!
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -357,6 +357,13 @@ def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
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_through_join_model_with_include
author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
assert_no_queries do
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -53,6 +53,7 @@ def add_joins_and_select
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
Expand Down

0 comments on commit 0963774

Please sign in to comment.