[Rails] Various ways to delete data

Simplified chart

Method Description Range to delete
destroy Delete one data Delete target data and related data
destroy_all Delete multiple data Delete target data and related data
delete Delete one data Delete only the target data
delete_all Delete multiple data Delete only the target data
Related data `dependent: Dependent data with: destroy`

How to use

python


#destroy method
User.find(1).destroy

# destroy_all method
User.where(id: 1..5).destroy_all

#delete method
User.find(1).delete

# delete_all method
User.where(id: 1..5).delete_all

Recommended Posts