Permalink
Browse files

Fix has_many :through to add the appropriate conditions when going th…

…rough an association using STI. Closes #5783. [Jonathan Viney]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5305 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 39963b4 commit cdb5132c411db0a330e85ec9d2eb7fe543af7aff @technoweenie technoweenie committed Oct 15, 2006
@@ -104,14 +104,29 @@ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_mult
end
def test_eager_with_has_many_through
- posts_with_comments = people(:michael).posts.find(:all, :include => :comments )
+ posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
posts_with_author = people(:michael).posts.find(:all, :include => :author )
posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ])
assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
end
+ def test_eager_with_has_many_through_an_sti_join_model
+ author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
+ assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
+ end
+
+ def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
+ author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
+ assert_equal [], author.special_nonexistant_post_comments
+ end
+
+ def test_eager_with_has_many_through_join_model_with_conditions
+ assert_equal Author.find(:first, :include => :hello_post_comments).hello_post_comments,
+ Author.find(:first).hello_post_comments
+ 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
@@ -463,6 +463,10 @@ def test_has_many_through_sum_uses_calculations
assert_nothing_raised { authors(:david).comments.sum(:post_id) }
end
+ def test_has_many_through_has_many_with_sti
+ assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
+ end
+
private
# create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency)
@@ -17,7 +17,15 @@ def testing_proxy_target
has_many :comments, :through => :posts
has_many :funky_comments, :through => :posts, :source => :comments
- has_many :special_posts, :class_name => "Post"
+ has_many :special_posts
+ has_many :special_post_comments, :through => :special_posts, :source => :comments
+
+ has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'"
+ has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => "comments.post_id = 0"
+
+ has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
+ has_many :hello_post_comments, :through => :hello_posts, :source => :comments
+
has_many :other_posts, :class_name => "Post"
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
:after_add => :log_after_adding,

0 comments on commit cdb5132

Please sign in to comment.