提交 02d3a253 编写于 作者: E eileencodes

[ci skip] Clarify deletion strategies for collection proxies

For detailed testing of behavior see:
https://gist.github.com/eileencodes/5b0a2fe011dcff6203fe

This shows destroy_all always destroys records and fires callbacks.
It will never use nullify or delete_all

delete_all's behavior varies greatly based on `hm` vs `hm:t` and deletion
strategy.
上级 a4d8b62e
...@@ -355,14 +355,15 @@ def replace(other_array) ...@@ -355,14 +355,15 @@ def replace(other_array)
@association.replace(other_array) @association.replace(other_array)
end end
# Deletes all the records from the collection. For +has_many+ associations, # Deletes all the records from the collection according to the strategy
# the deletion is done according to the strategy specified by the <tt>:dependent</tt> # specified by the +:dependent+ option. If no +:dependent+ option is given,
# option. # then it will follow the default strategy.
# #
# If no <tt>:dependent</tt> option is given, then it will follow the # For +has_many :through+ associations, the default deletion strategy is
# default strategy. The default strategy is <tt>:nullify</tt>. This # +:delete_all+.
# sets the foreign keys to <tt>NULL</tt>. For, +has_many+ <tt>:through</tt>, #
# the default 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 # class Person < ActiveRecord::Base
# has_many :pets # dependent: :nullify option by default # has_many :pets # dependent: :nullify option by default
...@@ -393,9 +394,9 @@ def replace(other_array) ...@@ -393,9 +394,9 @@ def replace(other_array)
# # #<Pet id: 3, name: "Choo-Choo", person_id: nil> # # #<Pet id: 3, name: "Choo-Choo", person_id: nil>
# # ] # # ]
# #
# If it is set to <tt>:destroy</tt> all the objects from the collection # Both +has_many+ and +has_many :through+ dependencies default to the
# are removed by calling their +destroy+ method. See +destroy+ for more # +:delete_all+ strategy if the +:dependent+ option is set to +:destroy+.
# information. # Records are not instantiated and callbacks will not be fired.
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# has_many :pets, dependent: :destroy # has_many :pets, dependent: :destroy
...@@ -410,11 +411,6 @@ def replace(other_array) ...@@ -410,11 +411,6 @@ def replace(other_array)
# # ] # # ]
# #
# person.pets.delete_all # person.pets.delete_all
# # => [
# # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
# # #<Pet id: 2, name: "Spook", person_id: 1>,
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
# #
# Pet.find(1, 2, 3) # Pet.find(1, 2, 3)
# # => ActiveRecord::RecordNotFound # # => ActiveRecord::RecordNotFound
...@@ -443,8 +439,9 @@ def delete_all(dependent = nil) ...@@ -443,8 +439,9 @@ def delete_all(dependent = nil)
end end
# Deletes the records of the collection directly from the database # Deletes the records of the collection directly from the database
# ignoring the +:dependent+ option. It invokes +before_remove+, # ignoring the +:dependent+ option. Records are instantiated and it
# +after_remove+ , +before_destroy+ and +after_destroy+ callbacks. # invokes +before_remove+, +after_remove+ , +before_destroy+ and
# +after_destroy+ callbacks.
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# has_many :pets # has_many :pets
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册