0
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 3 years have passed since last update.

saveとcreateについて

0
Posted at

1点目

saveはインスタンスメソッドでnewを記述してからでないと使えない。
createはクラスメソッドでnewの記述はいらない

2点目:保存の判定

saveの場合
[1] pry(main)> @item = Item.new #newを作って
   (0.7ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
=> #<Item:0x00007feeacdcfe68
 id: nil,
 item_name: nil,
 description: nil,
 category_id: nil,
 status_id: nil,
 delivery_fee_payment_id: nil,
 prefecture_id: nil,
 delivery_prepare_id: nil,
 price: nil,
 user_id: nil,
 created_at: nil,
 updated_at: nil>
[2] pry(main)> @item.save ≈
   (13.5ms)  BEGIN
   (12.5ms)  ROLLBACK
=> false
# true/falseを返す
createの場合
[3] pry(main)> Item.create
=> #<Item:0x00007feead0fed28
 id: nil,
 item_name: nil,
 description: nil,
 category_id: nil,
 status_id: nil,
 delivery_fee_payment_id: nil,
 prefecture_id: nil,
 delivery_prepare_id: nil,
 price: nil,
 user_id: nil,
 created_at: nil,
 updated_at: nil>
インスタンスで返ってくるのでデータの中身
0
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
0
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?