class ActiveRecord::Associations::CollectionProxy
def delete(*records)
# #
# #
# => [
person.pets.delete(2, 3)
# => [#
person.pets.delete("1")
# ]
# #
# #
# #
# => [
person.pets
person.pets.size # => 3
end
has_many :pets
class Person < ActiveRecord::Base
responding to the +id+ and executes delete on them.
You can pass +Integer+ or +String+ values, it finds the records
# => ActiveRecord::RecordNotFound: Couldn't find Pet with 'id'=1
Pet.find(1)
# ]
# #
# #
# => [
person.pets
person.pets.size # => 2
# => [#
person.pets.delete(Pet.find(1))
# ]
# #
# #
# #
# => [
person.pets
person.pets.size # => 3
end
has_many :pets, dependent: :delete_all
class Person < ActiveRecord::Base
*without* calling their +destroy+ method.
If it is set to :delete_all, all the +records+ are deleted
# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 3)
Pet.find(1, 3)
# => [#
person.pets
person.pets.size # => 1
# ]
# #
# #
# => [
person.pets.delete(Pet.find(1), Pet.find(3))
# ]
# #
# #
# #
# => [
person.pets
person.pets.size # => 3
end
has_many :pets, dependent: :destroy
class Person < ActiveRecord::Base
their +destroy+ method. See +destroy+ for more information.
If it is set to :destroy all the +records+ are removed by calling
# => #
Pet.find(1)
# ]
# #
# #
# => [
person.pets
person.pets.size # => 2
# => [#
person.pets.delete(Pet.find(1))
# ]
# #
# #
# #
# => [
person.pets
person.pets.size # => 3
end
has_many :pets # dependent: :nullify option by default
class Person < ActiveRecord::Base
This sets the foreign keys to +NULL+.
For +has_many+ associations, the default deletion strategy is +:nullify+.
+:delete_all+.
For has_many :through associations, the default deletion strategy is
deleted records.
then it will follow the default strategy. Returns an array with the
specified by the +:dependent+ option. If no +:dependent+ option is given,
Deletes the +records+ supplied from the collection according to the strategy
def delete(*records) @association.delete(*records).tap { reset_scope } end