Browse files

add CollectionProxy#clear documentation

  • Loading branch information...
1 parent 5111ec4 commit 3f46f73d004eb19616ad9863fefa4c21ef1d76c3 Francesco Rodriguez committed May 19, 2012
Showing with 26 additions and 0 deletions.
  1. +26 −0 activerecord/lib/active_record/associations/collection_proxy.rb
View
26 activerecord/lib/active_record/associations/collection_proxy.rb
@@ -243,6 +243,32 @@ def <<(*records)
end
alias_method :push, :<<
+ # Removes every object from the collection. This does not destroy
+ # the objects, it sets their foreign keys to +NULL+. Returns +self+
+ # so methods can be chained.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # person.pets # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
+ # person.pets.clear # => []
+ # person.pets.size # => 0
+ #
+ # Pet.find(1) # => #<Pet id: 1, name: "Snoop", group: "dogs", person_id: nil>
+ #
+ # If they are associated with +dependent: :destroy+ option, it deletes
+ # them directly from the database.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets, dependent: :destroy
+ # end
+ #
+ # person.pets # => [#<Pet id: 2, name: "Wy", group: "cats", person_id: 2>]
+ # person.pets.clear # => []
+ # person.pets.size # => 0
+ #
+ # Pet.find(2) # => ActiveRecord::RecordNotFound: Couldn't find Pet with id=2
def clear
delete_all
self

0 comments on commit 3f46f73

Please sign in to comment.