Skip to content

Commit

Permalink
[ci skip] fix the collection.clear guide
Browse files Browse the repository at this point in the history
Improve the guide about `has_many` `collection.clear` to indicate
the behavior for each dependent strategy according to
`collection.delete_all`.

Based on rails#17179, I changed the `collection.delete` docs to also
clarify the default strategy for each `hm` and `hm:t` associations.

Fixes rails#20170.
  • Loading branch information
repinel committed Jun 3, 2015
1 parent 8193a09 commit efa1648
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 8 additions & 7 deletions activerecord/lib/active_record/associations/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,16 @@ def destroy_all
@association.destroy_all
end

# Deletes the +records+ supplied and removes them from the collection. For
# +has_many+ associations, the deletion is done according to the strategy
# specified by the <tt>:dependent</tt> option. Returns an array with the
# Deletes the +records+ supplied from the collection according to the strategy
# specified by the +:dependent+ option. If no +:dependent+ option is given,
# then it will follow the default strategy. Returns an array with the
# deleted records.
#
# If no <tt>:dependent</tt> option is given, then it will follow the default
# strategy. The default strategy is <tt>:nullify</tt>. This sets the foreign
# keys to <tt>NULL</tt>. For, +has_many+ <tt>:through</tt>, the default
# strategy is +delete_all+.
# For +has_many :through+ associations, the default deletion strategy is
# +:delete_all+.
#
# For +has_many+ associations, the default deletion strategy is +:nullify+.
# This sets the foreign keys to +NULL+.
#
# class Person < ActiveRecord::Base
# has_many :pets # dependent: :nullify option by default
Expand Down
8 changes: 7 additions & 1 deletion guides/source/association_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,13 @@ The `collection_singular_ids=` method makes the collection contain only the obje

##### `collection.clear`

The `collection.clear` method removes every object from the collection. This destroys the associated objects if they are associated with `dependent: :destroy`, deletes them directly from the database if `dependent: :delete_all`, and otherwise sets their foreign keys to `NULL`.
The `collection.clear` method removes all objects from the collection according to the strategy specified by the `dependent` option. If no option is given, it follows the default strategy. The default strategy for `has_many :through` associations is `delete_all`, and for `has_many` associations is to set the foreign keys to `NULL`.

```ruby
@customer.orders.clear
```

WARNING: Objects will be delete if they're associated with `dependent: :destroy`, just like `dependent: :delete_all`.

##### `collection.empty?`

Expand Down

0 comments on commit efa1648

Please sign in to comment.