diff options
author | Rick Olson <technoweenie@gmail.com> | 2008-04-01 18:27:22 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2008-04-01 18:27:22 +0000 |
commit | e223216e9ac496d4dad53c7ada1406e9e15fbb55 (patch) | |
tree | c363adab1d0b7fcc7bfaea8b8c44e0f17ab61794 /activerecord/test/cases/associations_test.rb | |
parent | 4e07037afa7081538a4e6386464348f23d020585 (diff) | |
download | rails-e223216e9ac496d4dad53c7ada1406e9e15fbb55.tar.gz rails-e223216e9ac496d4dad53c7ada1406e9e15fbb55.tar.bz2 rails-e223216e9ac496d4dad53c7ada1406e9e15fbb55.zip |
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
Diffstat (limited to 'activerecord/test/cases/associations_test.rb')
-rwxr-xr-x | activerecord/test/cases/associations_test.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 5a9e32f774..3e2b104a13 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -1330,6 +1330,36 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions end + def test_include_uses_array_include_after_loaded + firm = companies(:first_firm) + client = firm.clients.first + + assert_no_queries do + assert firm.clients.loaded? + assert firm.clients.include?(client) + end + end + + def test_include_checks_if_record_exists_if_target_not_loaded + firm = companies(:first_firm) + client = firm.clients.first + + firm.reload + assert ! firm.clients.loaded? + assert_queries(1) do + assert firm.clients.include?(client) + end + assert ! firm.clients.loaded? + end + + def test_include_returns_false_for_non_matching_record_to_verify_scoping + firm = companies(:first_firm) + client = Client.create!(:name => 'Not Associated') + + assert ! firm.clients.loaded? + assert ! firm.clients.include?(client) + end + end class BelongsToAssociationsTest < ActiveRecord::TestCase @@ -2046,6 +2076,36 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal developers(:david), active_record.developers.find(developers(:david).id), "Ruby find" end + def test_include_uses_array_include_after_loaded + project = projects(:active_record) + developer = project.developers.first + + assert_no_queries do + assert project.developers.loaded? + assert project.developers.include?(developer) + end + end + + def test_include_checks_if_record_exists_if_target_not_loaded + project = projects(:active_record) + developer = project.developers.first + + project.reload + assert ! project.developers.loaded? + assert_queries(1) do + assert project.developers.include?(developer) + end + assert ! project.developers.loaded? + end + + def test_include_returns_false_for_non_matching_record_to_verify_scoping + project = projects(:active_record) + developer = Developer.create :name => "Bryan", :salary => 50_000 + + assert ! project.developers.loaded? + assert ! project.developers.include?(developer) + end + def test_find_in_association_with_custom_finder_sql assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find" |