diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-03-02 21:24:56 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-03-04 09:30:27 +0000 |
commit | 735844db712c511dd8abf36a5279318fbc0ff9d0 (patch) | |
tree | 5fbd5d224ef85d8c878bf221db98b422c9345466 /activerecord/test/cases/associations/nested_through_associations_test.rb | |
parent | 9a98c766e045aebc2ef6d5b716936b73407f095d (diff) | |
parent | b171b9e73dcc6a89b1da652da61c5127fe605b51 (diff) | |
download | rails-735844db712c511dd8abf36a5279318fbc0ff9d0.tar.gz rails-735844db712c511dd8abf36a5279318fbc0ff9d0.tar.bz2 rails-735844db712c511dd8abf36a5279318fbc0ff9d0.zip |
Merge branch 'master' into nested_has_many_through
Conflicts:
activerecord/CHANGELOG
activerecord/lib/active_record/association_preload.rb
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations/class_methods/join_dependency.rb
activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
activerecord/lib/active_record/associations/has_many_association.rb
activerecord/lib/active_record/associations/has_many_through_association.rb
activerecord/lib/active_record/associations/has_one_association.rb
activerecord/lib/active_record/associations/has_one_through_association.rb
activerecord/lib/active_record/associations/through_association_scope.rb
activerecord/lib/active_record/reflection.rb
activerecord/test/cases/associations/has_many_through_associations_test.rb
activerecord/test/cases/associations/has_one_through_associations_test.rb
activerecord/test/cases/reflection_test.rb
activerecord/test/cases/relations_test.rb
activerecord/test/fixtures/memberships.yml
activerecord/test/models/categorization.rb
activerecord/test/models/category.rb
activerecord/test/models/member.rb
activerecord/test/models/reference.rb
activerecord/test/models/tagging.rb
Diffstat (limited to 'activerecord/test/cases/associations/nested_through_associations_test.rb')
-rw-r--r-- | activerecord/test/cases/associations/nested_through_associations_test.rb | 258 |
1 files changed, 164 insertions, 94 deletions
diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index db7c8b6c45..a4ac69782a 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -51,9 +51,19 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase # Through: has_many def test_has_many_through_has_many_with_has_many_through_source_reflection general = tags(:general) - assert_equal [general, general], authors(:david).tags + end + + def test_has_many_through_has_many_with_has_many_through_source_reflection_preload + authors = assert_queries(5) { Author.includes(:tags).to_a } + general = tags(:general) + + assert_no_queries do + assert_equal [general, general], authors.first.tags + end + end + def test_has_many_through_has_many_with_has_many_through_source_reflection_preload_via_joins assert_includes_and_joins_equal( Author.where('tags.id' => tags(:general).id), [authors(:david)], :tags @@ -62,11 +72,6 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase # This ensures that the polymorphism of taggings is being observed correctly authors = Author.joins(:tags).where('taggings.taggable_type' => 'FakeModel') assert authors.empty? - - authors = assert_queries(5) { Author.includes(:tags).to_a } - assert_no_queries do - assert_equal [general, general], authors.first.tags - end end # has_many through @@ -74,60 +79,69 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase # Through: has_many through def test_has_many_through_has_many_through_with_has_many_source_reflection luke, david = subscribers(:first), subscribers(:second) + assert_equal [luke, david, david], authors(:david).subscribers.order('subscribers.nick') + end - author = authors(:david) - assert_equal [luke, david, david], author.subscribers.order('subscribers.nick') + def test_has_many_through_has_many_through_with_has_many_source_reflection_preload + luke, david = subscribers(:first), subscribers(:second) + authors = assert_queries(4) { Author.includes(:subscribers).to_a } + assert_no_queries do + assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick) + end + end + def test_has_many_through_has_many_through_with_has_many_source_reflection_preload_via_joins # All authors with subscribers where one of the subscribers' nick is 'alterself' assert_includes_and_joins_equal( Author.where('subscribers.nick' => 'alterself'), [authors(:david)], :subscribers ) - - authors = assert_queries(4) { Author.includes(:subscribers).to_a } - assert_no_queries do - assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick) - end end # has_many through # Source: has_one through # Through: has_one def test_has_many_through_has_one_with_has_one_through_source_reflection - founding = member_types(:founding) - - assert_equal [founding], members(:groucho).nested_member_types - - assert_includes_and_joins_equal( - Member.where('member_types.id' => founding.id), - [members(:groucho)], :nested_member_types - ) + assert_equal [member_types(:founding)], members(:groucho).nested_member_types + end + def test_has_many_through_has_one_with_has_one_through_source_reflection_preload members = assert_queries(4) { Member.includes(:nested_member_types).to_a } + founding = member_types(:founding) assert_no_queries do assert_equal [founding], members.first.nested_member_types end end + def test_has_many_through_has_one_with_has_one_through_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Member.where('member_types.id' => member_types(:founding).id), + [members(:groucho)], :nested_member_types + ) + end + # has_many through # Source: has_one # Through: has_one through def test_has_many_through_has_one_through_with_has_one_source_reflection - mustache = sponsors(:moustache_club_sponsor_for_groucho) - - assert_equal [mustache], members(:groucho).nested_sponsors - - assert_includes_and_joins_equal( - Member.where('sponsors.id' => mustache.id), - [members(:groucho)], :nested_sponsors - ) + assert_equal [sponsors(:moustache_club_sponsor_for_groucho)], members(:groucho).nested_sponsors + end + def test_has_many_through_has_one_through_with_has_one_source_reflection_preload members = assert_queries(4) { Member.includes(:nested_sponsors).to_a } + mustache = sponsors(:moustache_club_sponsor_for_groucho) assert_no_queries do assert_equal [mustache], members.first.nested_sponsors end end + def test_has_many_through_has_one_through_with_has_one_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Member.where('sponsors.id' => sponsors(:moustache_club_sponsor_for_groucho).id), + [members(:groucho)], :nested_sponsors + ) + end + # has_many through # Source: has_many through # Through: has_one @@ -136,7 +150,18 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase assert_equal [groucho_details, other_details], members(:groucho).organization_member_details.order('member_details.id') + end + + def test_has_many_through_has_one_with_has_many_through_source_reflection_preload + members = assert_queries(4) { Member.includes(:organization_member_details).to_a.sort_by(&:id) } + groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy) + + assert_no_queries do + assert_equal [groucho_details, other_details], members.first.organization_member_details.sort_by(&:id) + end + end + def test_has_many_through_has_one_with_has_many_through_source_reflection_preload_via_joins assert_includes_and_joins_equal( Member.where('member_details.id' => member_details(:groucho).id).order('member_details.id'), [members(:groucho), members(:some_other_guy)], :organization_member_details @@ -145,11 +170,6 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase members = Member.joins(:organization_member_details). where('member_details.id' => 9) assert members.empty? - - members = assert_queries(4) { Member.includes(:organization_member_details).to_a.sort_by(&:id) } - assert_no_queries do - assert_equal [groucho_details, other_details], members.first.organization_member_details.sort_by(&:id) - end end # has_many through @@ -160,20 +180,26 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase assert_equal [groucho_details, other_details], members(:groucho).organization_member_details_2.order('member_details.id') + end + + def test_has_many_through_has_one_through_with_has_many_source_reflection_preload + members = assert_queries(4) { Member.includes(:organization_member_details_2).to_a.sort_by(&:id) } + groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy) + assert_no_queries do + assert_equal [groucho_details, other_details], members.first.organization_member_details_2.sort_by(&:id) + end + end + + def test_has_many_through_has_one_through_with_has_many_source_reflection_preload_via_joins assert_includes_and_joins_equal( - Member.where('member_details.id' => groucho_details.id).order('member_details.id'), + Member.where('member_details.id' => member_details(:groucho).id).order('member_details.id'), [members(:groucho), members(:some_other_guy)], :organization_member_details_2 ) members = Member.joins(:organization_member_details_2). where('member_details.id' => 9) assert members.empty? - - members = assert_queries(4) { Member.includes(:organization_member_details_2).to_a.sort_by(&:id) } - assert_no_queries do - assert_equal [groucho_details, other_details], members.first.organization_member_details_2.sort_by(&:id) - end end # has_many through @@ -183,18 +209,24 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase general, cooking = categories(:general), categories(:cooking) assert_equal [general, cooking], authors(:bob).post_categories.order('categories.id') + end - assert_includes_and_joins_equal( - Author.where('categories.id' => cooking.id), - [authors(:bob)], :post_categories - ) - + def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload authors = assert_queries(3) { Author.includes(:post_categories).to_a.sort_by(&:id) } + general, cooking = categories(:general), categories(:cooking) + assert_no_queries do assert_equal [general, cooking], authors[2].post_categories.sort_by(&:id) end end + def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Author.where('categories.id' => categories(:cooking).id), + [authors(:bob)], :post_categories + ) + end + # has_many through # Source: has_many # Through: has_and_belongs_to_many @@ -202,18 +234,24 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase greetings, more = comments(:greetings), comments(:more_greetings) assert_equal [greetings, more], categories(:technology).post_comments.order('comments.id') + end - assert_includes_and_joins_equal( - Category.where('comments.id' => more.id).order('comments.id'), - [categories(:general), categories(:technology)], :post_comments - ) - + def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload categories = assert_queries(3) { Category.includes(:post_comments).to_a.sort_by(&:id) } + greetings, more = comments(:greetings), comments(:more_greetings) + assert_no_queries do assert_equal [greetings, more], categories[1].post_comments.sort_by(&:id) end end + def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Category.where('comments.id' => comments(:more_greetings).id).order('comments.id'), + [categories(:general), categories(:technology)], :post_comments + ) + end + # has_many through # Source: has_many through a habtm # Through: has_many through @@ -221,35 +259,45 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase greetings, more = comments(:greetings), comments(:more_greetings) assert_equal [greetings, more], authors(:bob).category_post_comments.order('comments.id') + end - assert_includes_and_joins_equal( - Author.where('comments.id' => comments(:does_it_hurt).id).order('comments.id'), - [authors(:david), authors(:mary)], :category_post_comments - ) - + def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload authors = assert_queries(5) { Author.includes(:category_post_comments).to_a.sort_by(&:id) } + greetings, more = comments(:greetings), comments(:more_greetings) + assert_no_queries do assert_equal [greetings, more], authors[2].category_post_comments.sort_by(&:id) end end + def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Author.where('comments.id' => comments(:does_it_hurt).id).order('comments.id'), + [authors(:david), authors(:mary)], :category_post_comments + ) + end + # has_many through # Source: belongs_to # Through: has_many through def test_has_many_through_has_many_through_with_belongs_to_source_reflection + assert_equal [tags(:general), tags(:general)], authors(:david).tagging_tags + end + + def test_has_many_through_has_many_through_with_belongs_to_source_reflection_preload + authors = assert_queries(5) { Author.includes(:tagging_tags).to_a } general = tags(:general) - assert_equal [general, general], authors(:david).tagging_tags + assert_no_queries do + assert_equal [general, general], authors.first.tagging_tags + end + end + def test_has_many_through_has_many_through_with_belongs_to_source_reflection_preload_via_joins assert_includes_and_joins_equal( Author.where('tags.id' => tags(:general).id), [authors(:david)], :tagging_tags ) - - authors = assert_queries(5) { Author.includes(:tagging_tags).to_a } - assert_no_queries do - assert_equal [general, general], authors.first.tagging_tags - end end # has_many through @@ -260,54 +308,68 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase assert_equal [welcome_general, thinking_general], categorizations(:david_welcome_general).post_taggings.order('taggings.id') + end - assert_includes_and_joins_equal( - Categorization.where('taggings.id' => welcome_general.id).order('taggings.id'), - [categorizations(:david_welcome_general)], :post_taggings - ) - + def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload categorizations = assert_queries(4) { Categorization.includes(:post_taggings).to_a.sort_by(&:id) } + welcome_general, thinking_general = taggings(:welcome_general), taggings(:thinking_general) + assert_no_queries do assert_equal [welcome_general, thinking_general], categorizations.first.post_taggings.sort_by(&:id) end end + def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload_via_joins + assert_includes_and_joins_equal( + Categorization.where('taggings.id' => taggings(:welcome_general).id).order('taggings.id'), + [categorizations(:david_welcome_general)], :post_taggings + ) + end + # has_one through # Source: has_one through # Through: has_one def test_has_one_through_has_one_with_has_one_through_source_reflection + assert_equal member_types(:founding), members(:groucho).nested_member_type + end + + def test_has_one_through_has_one_with_has_one_through_source_reflection_preload + members = assert_queries(4) { Member.includes(:nested_member_type).to_a.sort_by(&:id) } founding = member_types(:founding) - assert_equal founding, members(:groucho).nested_member_type + assert_no_queries do + assert_equal founding, members.first.nested_member_type + end + end + def test_has_one_through_has_one_with_has_one_through_source_reflection_preload_via_joins assert_includes_and_joins_equal( - Member.where('member_types.id' => founding.id), + Member.where('member_types.id' => member_types(:founding).id), [members(:groucho)], :nested_member_type ) - - members = assert_queries(4) { Member.includes(:nested_member_type).to_a.sort_by(&:id) } - assert_no_queries do - assert_equal founding, members.first.nested_member_type - end end # has_one through # Source: belongs_to # Through: has_one through def test_has_one_through_has_one_through_with_belongs_to_source_reflection + assert_equal categories(:general), members(:groucho).club_category + end + + def test_has_one_through_has_one_through_with_belongs_to_source_reflection_preload + members = assert_queries(4) { Member.includes(:club_category).to_a.sort_by(&:id) } general = categories(:general) - assert_equal general, members(:groucho).club_category + assert_no_queries do + assert_equal general, members.first.club_category + end + end + def test_has_one_through_has_one_through_with_belongs_to_source_reflection_preload_via_joins assert_includes_and_joins_equal( Member.where('categories.id' => categories(:technology).id), [members(:blarpy_winkup)], :club_category ) - - members = assert_queries(4) { Member.includes(:club_category).to_a.sort_by(&:id) } - assert_no_queries do - assert_equal general, members.first.club_category - end end def test_distinct_has_many_through_a_has_many_through_association_on_source_reflection @@ -406,41 +468,49 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase end def test_nested_has_many_through_with_conditions_on_through_associations - blue, bob = tags(:blue), authors(:bob) - - assert_equal [blue], bob.misc_post_first_blue_tags - - # Pointless condition to force single-query loading - assert_includes_and_joins_equal( - Author.where('tags.id = tags.id'), - [bob], :misc_post_first_blue_tags - ) + assert_equal [tags(:blue)], authors(:bob).misc_post_first_blue_tags + end + def test_nested_has_many_through_with_conditions_on_through_associations_preload assert Author.where('tags.id' => 100).joins(:misc_post_first_blue_tags).empty? authors = assert_queries(3) { Author.includes(:misc_post_first_blue_tags).to_a.sort_by(&:id) } + blue = tags(:blue) + assert_no_queries do assert_equal [blue], authors[2].misc_post_first_blue_tags end end - def test_nested_has_many_through_with_conditions_on_source_associations - blue, bob = tags(:blue), authors(:bob) - - assert_equal [blue], bob.misc_post_first_blue_tags_2 - + def test_nested_has_many_through_with_conditions_on_through_associations_preload_via_joins # Pointless condition to force single-query loading assert_includes_and_joins_equal( Author.where('tags.id = tags.id'), - [bob], :misc_post_first_blue_tags_2 + [authors(:bob)], :misc_post_first_blue_tags ) + end + + def test_nested_has_many_through_with_conditions_on_source_associations + assert_equal [tags(:blue)], authors(:bob).misc_post_first_blue_tags_2 + end + def test_nested_has_many_through_with_conditions_on_source_associations_preload authors = assert_queries(4) { Author.includes(:misc_post_first_blue_tags_2).to_a.sort_by(&:id) } + blue = tags(:blue) + assert_no_queries do assert_equal [blue], authors[2].misc_post_first_blue_tags_2 end end + def test_nested_has_many_through_with_conditions_on_source_associations_preload_via_joins + # Pointless condition to force single-query loading + assert_includes_and_joins_equal( + Author.where('tags.id = tags.id'), + [authors(:bob)], :misc_post_first_blue_tags_2 + ) + end + def test_nested_has_many_through_with_foreign_key_option_on_the_source_reflection_through_reflection assert_equal [categories(:general)], organizations(:nsa).author_essay_categories |