From e223216e9ac496d4dad53c7ada1406e9e15fbb55 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Tue, 1 Apr 2008 18:27:22 +0000 Subject: Add efficient #include? to AssociationCollection (for has_many/has_many :through/habtm). [stopdropandrew] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9200 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/cases/associations/join_model_test.rb | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index d4db4ef770..2b929d4480 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -639,7 +639,37 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_equal comments.first.post, comments[1].post end end + + def test_has_many_through_include_uses_array_include_after_loaded + david = authors(:david) + category = david.categories.first + + assert_no_queries do + assert david.categories.loaded? + assert david.categories.include?(category) + end + end + + def test_has_many_through_include_checks_if_record_exists_if_target_not_loaded + david = authors(:david) + category = david.categories.first + + david.reload + assert ! david.categories.loaded? + assert_queries(1) do + assert david.categories.include?(category) + end + assert ! david.categories.loaded? + end + def test_has_many_through_include_returns_false_for_non_matching_record_to_verify_scoping + david = authors(:david) + category = Category.create!(:name => 'Not Associated') + + assert ! david.categories.loaded? + assert ! david.categories.include?(category) + end + private # create dynamic Post models to allow different dependency options def find_post_with_dependency(post_id, association, association_name, dependency) -- cgit v1.2.3