diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 682869e5852243250ab69d78b74da32fb2f33c00..069d6a3841752b0cc6f16257ef3502a3477de13d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add efficient #include? to AssociationCollection (for has_many/has_many :through/habtm). [stopdropandrew] + * PostgreSQL: create_ and drop_database support. #9042 [ez, pedz, nicksieger] * Ensure that validates_uniqueness_of works with with_scope. Closes #9235. [nik.wakelin, cavalle] diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index d8b988d889fae7bdad18484821da72bae24e5900..3b8954e435408f198fc887b9fa3512be5c4ae87b 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -159,6 +159,11 @@ def replace(other_array) end end + def include?(record) + return false unless record.is_a?(@reflection.klass) + return @target.include?(record) if loaded? + exists?(record) + end protected def method_missing(method, *args) diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 83c9865205bb4c1455b18d948e0e0b73c91e6818..7d418ba701f3adbb1ff6ee2cc862dfc14c5dc424 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -1,6 +1,6 @@ module ActiveRecord module Associations - class HasManyThroughAssociation < AssociationProxy #:nodoc: + class HasManyThroughAssociation < AssociationCollection #:nodoc: def initialize(owner, reflection) super reflection.check_validity! diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index d4db4ef77007bf151bc47f23896e1ccf8df31c9b..2b929d4480f0d812ecbecd8fd5e8efa494bff608 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -639,7 +639,37 @@ def test_belongs_to_shared_parent 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) diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 5a9e32f774c1ba0a119486f659fb40438d054f8b..3e2b104a13ea9897ad8bf52481abbabf460aba23 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -1330,6 +1330,36 @@ def test_has_many_through_respects_hash_conditions 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 @@ def test_find_in_association 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"