6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】データを削除する様々な方法

Posted at

#早見表

メソッド 説明 削除する範囲
destroy 一件のデータを削除 対象データと関連データを削除
destroy_all 複数データを削除 対象データと関連データを削除
delete 一件のデータを削除 対象データのみ削除
delete_all 複数データを削除 対象データのみ削除
関連するデータ`dependent: :destroy`で依存関係にあるデータのこと

#使い方

# destroyメソッド
User.find(1).destroy

# destroy_allメソッド
User.where(id: 1..5).destroy_all

# deleteメソッド
User.find(1).delete

# delete_allメソッド
User.where(id: 1..5).delete_all
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?