Permalink
Browse files

Eager loading :through associations will join the :source model if th…

…ere are :conditions. [#2362 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent e848ab5 commit 17650e394fae26984d506fd0f705bc32e5a5de27 Grant Ammons committed with josevalim Jun 28, 2010
@@ -282,7 +282,12 @@ def preload_through_records(records, reflection, through_association)
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
@@ -357,6 +357,12 @@ def test_eager_with_has_many_through_join_model_with_include
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
@@ -58,6 +58,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

0 comments on commit 17650e3

Please sign in to comment.