Rails delete_all 与 destroy_all的区别

2017-10-31 20:21:38来源:CSDN作者:axu_air人点击

分享

delete(*records)

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.

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 defaultendperson.pets.size # => 3person.pets# => [#       #<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>#    ]person.pets.delete(Pet.find(1))# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]person.pets.size # => 2person.pets# => [#       #<Pet id: 2, name: "Spook", person_id: 1>,#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>#    ]Pet.find(1)# => #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>

delete_all(dependent = nil)

Deletes all the records 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.

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 defaultendperson.pets.size # => 3person.pets# => [#       #<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>#    ]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>#    ]person.pets.size # => 0person.pets      # => []Pet.find(1, 2, 3)# => [#       #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>,#       #<Pet id: 2, name: "Spook", person_id: nil>,#       #<Pet id: 3, name: "Choo-Choo", person_id: nil>#    ]Both has_many and has_many :through dependencies default to the :delete_all strategy if the :dependent option is set to :destroy. Records are not instantiated and callbacks will not be fired.class Person < ActiveRecord::Base  has_many :pets, dependent: :destroyendperson.pets.size # => 3person.pets# => [#       #<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>#    ]person.pets.delete_allPet.find(1, 2, 3)# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)If it is set to :delete_all, all the objects are deleted without calling their destroy method.class Person < ActiveRecord::Base  has_many :pets, dependent: :delete_allendperson.pets.size # => 3person.pets# => [#       #<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>#    ]person.pets.delete_allPet.find(1, 2, 3)# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)

destroy(*records)

Destroys the records supplied and removes them from the collection. This method will always remove record from the database ignoring the :dependent option. Returns an array with the removed records.

class Person < ActiveRecord::Base  has_many :petsendperson.pets.size # => 3person.pets# => [#       #<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>#    ]person.pets.destroy(Pet.find(1))# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]person.pets.size # => 2person.pets# => [#       #<Pet id: 2, name: "Spook", person_id: 1>,#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>#    ]person.pets.destroy(Pet.find(2), Pet.find(3))# => [#       #<Pet id: 2, name: "Spook", person_id: 1>,#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>#    ]person.pets.size  # => 0person.pets       # => []Pet.find(1, 2, 3) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)You can pass Integer or String values, it finds the records responding to the id and then deletes them from the database.person.pets.size # => 3person.pets# => [#       #<Pet id: 4, name: "Benny", person_id: 1>,#       #<Pet id: 5, name: "Brain", person_id: 1>,#       #<Pet id: 6, name: "Boss",  person_id: 1>#    ]person.pets.destroy("4")# => #<Pet id: 4, name: "Benny", person_id: 1>person.pets.size # => 2person.pets# => [#       #<Pet id: 5, name: "Brain", person_id: 1>,#       #<Pet id: 6, name: "Boss",  person_id: 1>#    ]person.pets.destroy(5, 6)# => [#       #<Pet id: 5, name: "Brain", person_id: 1>,#       #<Pet id: 6, name: "Boss",  person_id: 1>#    ]person.pets.size  # => 0person.pets       # => []Pet.find(4, 5, 6) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (4, 5, 6)Source: show | on GitHubdestroy_all()LinkDeletes the records of the collection directly from the database ignoring the :dependent option. Records are instantiated and it invokes before_remove, after_remove , before_destroy and after_destroy callbacks.class Person < ActiveRecord::Base  has_many :petsendperson.pets.size # => 3person.pets# => [#       #<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>#    ]person.pets.destroy_allperson.pets.size # => 0person.pets      # => []Pet.find(1) # => Couldn't find Pet with id=1

———————————–还没写完先保存一下————————–

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台