1
0

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 1 year has passed since last update.

create, saveの違い ,!のあるなしの違い , update, assign_attributes , destroy

Last updated at Posted at 2023-02-24

createとsaveの違いは、簡単にいうと保存するステップが違います。

○create

$Article.create({title: 'aaaaa', content: 'bbbbbb'})

と入力すると、データが作成される。

createはこのまま作成することができる。

○save

saveの場合は、newを使いインスタンスを作成する必要がある。
$article = Article.new({title: 'new', content: 'new'})
=> #<Article id: nil, title: "new", content: "new", created_at: nil, updated_at: nil>

現在idがnill なので、保存されていない状態
created_atも保存されていない状態

$article.save

を行うと初めて保存される。

$Article.last
=> #<Article id: 37, title: "new", content: "new", created_at: "2023-02-24 17:42:28", updated_at: "2023-02-24 17:42:28">

先ほど保存したのが取れていることを確認できる。

○まとめ

createとsaveは、ステップが違う(流れ)。 creataは、一発で作る。 セーブは、newで入れ物を作ったのちデータを入れてから、保存をする。

◎!あるなしの違い

先ほど説明したcreateには2つの種類があります。 create と create! です。

○create

$Article.create(title: ‘aaaaa’)
=> #<Article id: nil, title: "aaaa", content: nil, created_at: nil, updated_at: nil>

※ハッシュ省力することができます。
と入力した場合、contentがないため、保存はされません。

○create!

$Article.create!(title: 'aaaa')
Traceback (most recent call last):
    1: from (irb):49
ActiveRecord::RecordInvalid (バリデーションに失敗しました: 内容を入力してください)

と例外が発生しましたとなり、処理が止まってしまいます!

createの場合は、止まらずに普通に動いてしまう。

対保存したい、保存しないとダメなんだ。そんな時は、!をつけてあげよう♪

○saveでも一緒

$article = Article.new({title: 'aaaa'})
=> #<Article id: nil, title: "aaaa", content: nil, created_at: nil, updated_at: nil>

$article.save
=> false


$article.save!
Traceback (most recent call last):
    1: from (irb):55
ActiveRecord::RecordInvalid (バリデーションに失敗しました: 内容を入力してください)

!をつけると同じような表示がでて、 処理が止まる。

!をつけないと保存されなかったよと返ってくるだけで、問題を検知しにくい。

使いわけとしては、
・普段ユーザがミスしただけでは止まらないようにする時は、
!なし
・絶対に保存しなければいけないときは、!をつける
状況に応じて使い分けること!

◎update, assign_attributes

○update

$Article.first
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 8, title: "新しい記事だよ", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-22 10:09:36">

とりあえず記事をとってみる

例えばこうすると

$Article.first.update({title: 'updated'})
  Article Load (0.2ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
   (0.1ms)  begin transaction
  Article Update (1.2ms)  UPDATE "articles" SET "title" = ?, "updated_at" = ? WHERE "articles"."id" = ?  [["title", "updated"], ["updated_at", "2023-02-24 18:32:19.168756"], ["id", 8]]
   (0.7ms)  commit transaction
=> true

もう一度確認すると

$Article.first
  Article Load (0.2ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 8, title: "updated", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:32:19">

となりタイトルが変更されたのを確認できます👍

○assign_attributes

$article = Article.first
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 8, title: "updated", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:32:19"

$article.assign_attributes({title: 'assigned'})
=> nil

これでarticleをとると

$article
=> #<Article id: 8, title: "assigned", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:32:19">

タイトルがassignedになっている。
ただこの状態だと、インスタンスのタイトルを書き換えただけの状態
そのため、まだ保存されていない!

$article.save
   (0.1ms)  begin transaction
  Article Update (0.3ms)  UPDATE "articles" SET "title" = ?, "updated_at" = ? WHERE "articles"."id" = ?  [["title", "assigned"], ["updated_at", "2023-02-24 18:39:58.576106"], ["id", 8]]
   (0.5ms)  commit transaction
=> true

上記を実行後

$Article.first
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 8, title: "assigned", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:39:58">

確認するとタイトルが変更されている!

○まとめ

データの保存の仕方には2種類ある!!

create,update <=> new, assign_attributes -> save

左記は、すぐに変更されるパターン
右記は、newなどを実行後saveする

updateにもupdate,update!
がある。

こういった、更新系には、!あるVerとなしVerがあります👍

○destroy

destroyとは、 レコードを削除するためのもの
$article = Article.first
  Article Load (0.5ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 8, title: "assigned", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:39:58">

レコードをまず取得。

このarticleを削除したい場合、

$article.destroy

と実行。

$Article.first
  Article Load (0.2ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Article id: 9, title: "素晴らしい記事だよ", content: "ああああああああああああああああああああああああああああああ", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-07 21:28:07">

その後、上記で確認すると、初めのレコードが消えているがわかります。

$article.destroy!
=> #<Article id: 8, title: "assigned", content: "素晴らしい記事です。これは大変すごいです。ww", created_at: "2023-02-07 21:28:07", updated_at: "2023-02-24 18:39:58">

こういったのもあります。
絶対に削除されないと例外が発生する。

!ない場合で、失敗した場合は、falseが返ってくる。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?