aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-10-15 16:37:11 +0000
committerRick Olson <technoweenie@gmail.com>2006-10-15 16:37:11 +0000
commitcdb5132c411db0a330e85ec9d2eb7fe543af7aff (patch)
tree92114f1a60fc65e8b3f9a4bc5d7ab60a6f4b47e4
parent39963b4b9df7f6c3e7d29c91dfdd1e8279f51e60 (diff)
downloadrails-cdb5132c411db0a330e85ec9d2eb7fe543af7aff.tar.gz
rails-cdb5132c411db0a330e85ec9d2eb7fe543af7aff.tar.bz2
rails-cdb5132c411db0a330e85ec9d2eb7fe543af7aff.zip
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
-rw-r--r--activerecord/test/associations/eager_test.rb17
-rw-r--r--activerecord/test/associations/join_model_test.rb4
-rw-r--r--activerecord/test/fixtures/author.rb10
3 files changed, 29 insertions, 2 deletions
diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb
index d6e795b1fe..909083fd28 100644
--- a/activerecord/test/associations/eager_test.rb
+++ b/activerecord/test/associations/eager_test.rb
@@ -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
diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb
index 2c7902a695..1124a0ace0 100644
--- a/activerecord/test/associations/join_model_test.rb
+++ b/activerecord/test/associations/join_model_test.rb
@@ -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)
diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb
index 7373639383..6b34c7522c 100644
--- a/activerecord/test/fixtures/author.rb
+++ b/activerecord/test/fixtures/author.rb
@@ -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,