ChatGPT解决这个技术问题 Extra ChatGPT

How do you delete an ActiveRecord object?

How do you delete an ActiveRecord object?

I looked at Active Record Querying and it does not have anything on deleting that I can see.

Delete by id, Delete the current object like: user.remove, Can you delete based on a where clause?


Q
Qortex

It's destroy and destroy_all methods, like

user.destroy
User.find(15).destroy
User.destroy(15)
User.where(age: 20).destroy_all
User.destroy_all(age: 20)

Alternatively you can use delete and delete_all which won't enforce :before_destroy and :after_destroy callbacks or any dependent association options.

User.delete_all(condition: 'value') will allow you to delete records without a primary key

Note: from @hammady's comment, user.destroy won't work if User model has no primary key.

Note 2: From @pavel-chuchuva's comment, destroy_all with conditions and delete_all with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html


It would be nice if you could include @hammady's comment in your answer. Struggled to find out why I couldn't destroy my model object...
Why it only updates deleted_at column data in my database? How can I delete the whole row of data?
destroy_all with conditions and delete_all with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html
this User.find_by(username:"bob") is good to identify the record to destroy or delete, too.
C
Community

There is delete, delete_all, destroy, and destroy_all.

The docs are: older docs and Rails 3.0.0 docs

delete doesn't instantiate the objects, while destroy does. In general, delete is faster than destroy.


delete is faster but bypasses the callbacks you might have defined on the model
P
Pawel

User.destroy

User.destroy(1) will delete user with id == 1 and :before_destroy and :after_destroy callbacks occur. For example if you have associated records

has_many :addresses, :dependent => :destroy

After user is destroyed his addresses will be destroyed too. If you use delete action instead, callbacks will not occur.

User.destroy, User.delete User.destroy_all() or User.delete_all()

Notice: User is a class and user is an instance object


Thanks for addressing associated records.
BEWARE: User.destroy_all() runs callbacks, so before it deletes anything, it loads records. It's two SQL statements not one. Aside from the performance implications, this has concurrency implications too. The safer call skips callbacks; User.delete_all() will issue just a single DELETE FROM... command.
destroy_all with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html
D
Dhivya Dandapani

If you are using Rails 5 and above, the following solution will work.

#delete based on id
user_id = 50
User.find(id: user_id).delete_all

#delete based on condition
threshold_age = 20
User.where(age: threshold_age).delete_all

https://www.rubydoc.info/docs/rails/ActiveRecord%2FNullRelation:delete_all


M
Martins

Deleting a row from a particular table or a record set from a table is pretty simple, from your console all you need to do is grab the record set by its Id then delete or destroy it.