Skip to content

Commit 6f1d9d0

Browse files
Merge pull request rails#6511 from frodsan/add_fixnum_string_support_for_delete
Add support for CollectionAssociation#delete by Fixnum or String
2 parents ef3e696 + 39f0698 commit 6f1d9d0

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Added support to `CollectionAssociation#delete` for passing `fixnum`
4+
or `string` values as record ids. This finds the records responding
5+
to the `id` and executes delete on them.
6+
7+
class Person < ActiveRecord::Base
8+
has_many :pets
9+
end
10+
11+
person.pets.delete("1") # => [#<Pet id: 1>]
12+
person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>]
13+
14+
*Francesco Rodriguez*
15+
316
* Deprecated most of the 'dynamic finder' methods. All dynamic methods
417
except for `find_by_...` and `find_by_...!` are deprecated. Here's
518
how you can rewrite the code:

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def delete(*records)
230230
delete_records(:all, dependent)
231231
end
232232
else
233+
records = find(records) if records.any? { |record| record.kind_of?(Fixnum) || record.kind_of?(String) }
233234
delete_or_destroy(records, dependent)
234235
end
235236
end

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ class CollectionProxy < Relation
469469
#
470470
# :call-seq:
471471
# delete(*records)
472+
# delete(*fixnum_ids)
473+
# delete(*string_ids)
472474
#
473475
# Deletes the +records+ supplied and removes them from the collection. For
474476
# +has_many+ associations, the deletion is done according to the strategy
@@ -560,6 +562,30 @@ class CollectionProxy < Relation
560562
#
561563
# Pet.find(1)
562564
# # => ActiveRecord::RecordNotFound: Couldn't find Pet with id=1
565+
#
566+
# You can pass +Fixnum+ or +String+ values, it finds the records
567+
# responding to the +id+ and executes delete on them.
568+
#
569+
# class Person < ActiveRecord::Base
570+
# has_many :pets
571+
# end
572+
#
573+
# person.pets.size # => 3
574+
# person.pets
575+
# # => [
576+
# # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
577+
# # #<Pet id: 2, name: "Spook", person_id: 1>,
578+
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
579+
# # ]
580+
#
581+
# person.pets.delete("1")
582+
# # => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]
583+
#
584+
# person.pets.delete(2, 3)
585+
# # => [
586+
# # #<Pet id: 2, name: "Spook", person_id: 1>,
587+
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
588+
# # ]
563589

564590
##
565591
# :method: destroy

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,24 @@ def test_deleting_a_item_which_is_not_in_the_collection
936936
assert_equal 2, summit.client_of
937937
end
938938

939-
def test_deleting_type_mismatch
939+
def test_deleting_by_fixnum_id
940940
david = Developer.find(1)
941-
david.projects.reload
942-
assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
941+
942+
assert_difference 'david.projects.count', -1 do
943+
assert_equal 1, david.projects.delete(1).size
944+
end
945+
946+
assert_equal 1, david.projects.size
947+
end
948+
949+
def test_deleting_by_string_id
950+
david = Developer.find(1)
951+
952+
assert_difference 'david.projects.count', -1 do
953+
assert_equal 1, david.projects.delete('1').size
954+
end
955+
956+
assert_equal 1, david.projects.size
943957
end
944958

945959
def test_deleting_self_type_mismatch

0 commit comments

Comments
 (0)