diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c1926a20c77fa07dd00734f0488f6cd06dc2c351..6478436bd07f0ce3272b0cc385c293f9a0de11b1 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,6 +1,8 @@ *1.12.1* (October 19th, 2005) -* HABTM finders shouldn't return readonly records. #2525 [patrick@lenz.sh] +* Fixed Association#clear for associations which have not yet been accessed. #2524 [Patrick Lenz ] + +* HABTM finders shouldn't return readonly records. #2525 [Patrick Lenz ] * Make all tests runnable on their own. #2521. [Blair Zajac ] diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index ced734cfcd2ee7d528605dda2d553b940b0f9ef3..164d11b2d12fd5614c83a344eaee52dcf6e9b440 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -59,7 +59,7 @@ def delete(*records) # Removes all records from this association. Returns +self+ so method calls may be chained. def clear - return self if empty? # forces load_target if hasn't happened already + return self if length.zero? # forces load_target if hasn't happened already if @options[:exclusively_dependent] destroy_all else diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 47562f9176a820a3a7aa1cddcc7839666a640aa9..2d7ebc2b42030c993fcf5663d7159ea1fc2a5de8 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -566,6 +566,15 @@ def test_clearing_an_exclusively_dependent_association_collection assert Client.find_by_id(client_id).nil? end + def test_clearing_without_initial_access + firm = companies(:first_firm) + + firm.clients_of_firm.clear + + assert_equal 0, firm.clients_of_firm.size + assert_equal 0, firm.clients_of_firm(true).size + end + def test_deleting_a_item_which_is_not_in_the_collection force_signal37_to_load_all_clients_of_firm summit = Client.find_first("name = 'Summit'")