diff options
author | Pivotal Labs <contact@pivotallabs.com> | 2008-09-23 13:32:17 -0700 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-09-24 13:26:06 +0200 |
commit | 487758b3b88a38da3a75900839aea03774904fe1 (patch) | |
tree | e24091c573d18f80dde8b8dc8c214d4741515b37 /activerecord/test/cases | |
parent | 70b8ea4fa6f432919340345ae0d5af6aa8f87ec8 (diff) | |
download | rails-487758b3b88a38da3a75900839aea03774904fe1.tar.gz rails-487758b3b88a38da3a75900839aea03774904fe1.tar.bz2 rails-487758b3b88a38da3a75900839aea03774904fe1.zip |
Allowed passing arrays-of-strings to :join everywhere. Merge duplicate join strings to avoid table aliasing problems.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1077 state:committed]
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/method_scoping_test.rb | 30 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 6 |
3 files changed, 47 insertions, 0 deletions
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 10f6aa0f2a..292b88edbc 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -935,6 +935,17 @@ class FinderTest < ActiveRecord::TestCase assert_equal 1, first.id end + def test_joins_with_string_array + person_with_reader_and_post = Post.find( + :all, + :joins => [ + "INNER JOIN categorizations ON categorizations.post_id = posts.id", + "INNER JOIN categories ON categories.id = categorizations.category_id AND categories.type = 'SpecialCategory'" + ] + ) + assert_equal 1, person_with_reader_and_post.size + end + def test_find_by_id_with_conditions_with_or assert_nothing_raised do Post.find([1,2,3], diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index af6fcd32ad..ff10bfaf3e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -138,6 +138,36 @@ class MethodScopingTest < ActiveRecord::TestCase assert_equal authors(:david).attributes, scoped_authors.first.attributes end + def test_scoped_find_merges_string_array_style_and_string_style_joins + scoped_authors = Author.with_scope(:find => { :joins => ["INNER JOIN posts ON posts.author_id = authors.id"]}) do + Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1') + end + assert scoped_authors.include?(authors(:david)) + assert !scoped_authors.include?(authors(:mary)) + assert_equal 1, scoped_authors.size + assert_equal authors(:david).attributes, scoped_authors.first.attributes + end + + def test_scoped_find_merges_string_array_style_and_hash_style_joins + scoped_authors = Author.with_scope(:find => { :joins => :posts}) do + Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN comments ON posts.id = comments.post_id'], :conditions => 'comments.id = 1') + end + assert scoped_authors.include?(authors(:david)) + assert !scoped_authors.include?(authors(:mary)) + assert_equal 1, scoped_authors.size + assert_equal authors(:david).attributes, scoped_authors.first.attributes + end + + def test_scoped_find_merges_joins_and_eliminates_duplicate_string_joins + scoped_authors = Author.with_scope(:find => { :joins => 'INNER JOIN posts ON posts.author_id = authors.id'}) do + Author.find(:all, :select => 'DISTINCT authors.*', :joins => ["INNER JOIN posts ON posts.author_id = authors.id", "INNER JOIN comments ON posts.id = comments.post_id"], :conditions => 'comments.id = 1') + end + assert scoped_authors.include?(authors(:david)) + assert !scoped_authors.include?(authors(:mary)) + assert_equal 1, scoped_authors.size + assert_equal authors(:david).attributes, scoped_authors.first.attributes + end + def test_scoped_count_include # with the include, will retrieve only developers for the given project Developer.with_scope(:find => { :include => :projects }) do diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 444debd255..64e899780c 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -271,4 +271,10 @@ class NamedScopeTest < ActiveRecord::TestCase topics.size # use loaded (no query) end end + + def test_chaining_with_duplicate_joins + join = "INNER JOIN comments ON comments.post_id = posts.id" + post = Post.find(1) + assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size + end end |