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?
rails-activerecordruby-on-rails
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?
Best Answer
It's
destroy
anddestroy_all
methods, likeAlternatively you can use
delete
anddelete_all
which won't enforce:before_destroy
and:after_destroy
callbacks or any dependent association options.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 anddelete_all
with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html