mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5305 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
39963b4b9d
commit
cdb5132c41
3 changed files with 29 additions and 2 deletions
|
@ -104,7 +104,7 @@ class EagerAssociationTest < Test::Unit::TestCase
|
|||
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 }
|
||||
|
@ -112,6 +112,21 @@ class EagerAssociationTest < Test::Unit::TestCase
|
|||
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 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
10
activerecord/test/fixtures/author.rb
vendored
10
activerecord/test/fixtures/author.rb
vendored
|
@ -17,7 +17,15 @@ class Author < ActiveRecord::Base
|
|||
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,
|
||||
|
|
Loading…
Reference in a new issue