LoginSignup
0
0

More than 1 year has passed since last update.

rails cでの記述map,ppについて

Posted at

残しておきたい内容

メンターの方に質問した際にrails cをしている際に気になる記述をしていたので調べて自分の記録用として残しておこうと思います。

map

each文のようにモデルの内容を順番に表示できる
配列変数.map {|変数名| 具体的な処理 }

2.6.3 :002 > Dish.all.map{|d|d.dish_image}
2.6.3 :002 > Dish.all.map{|d|d.dish_image}
  Dish Load (0.2ms)  SELECT "dishes".* FROM "dishes"
 => [#<ActiveStorage::Attached::One:0x000000000394ec08 @name="dish_image", @record=#<Dish id: 8, created_at: "2022-04-11 08:38:04", updated_at: "2022-04-11 08:38:04", user_id: 6, dish_name: "エビチリ", introduction: "エビチリ">, @dependent=:purge_later>, #<ActiveStorage::Attached::One:0x000000000394eb18 @name="dish_image", @record=#<Dish id: 9, created_at: "2022-04-11 09:05:20", updated_at: "2022-04-11 09:05:20", user_id: 6, dish_name: "エビチリ", introduction: "qq">, @dependent=:purge_later>, #<ActiveStorage::Attached::One:0x000000000394ea28 @name="dish_image", @record=#<Dish id: 10, created_at: "2022-04-11 09:12:14", updated_at: "2022-04-11 09:12:14", user_id: 6, dish_name: "エビチリ", introduction: "エニチリです">, @dependent=:purge_later>, #<ActiveStorage::Attached::One:0x000000000394e938 @name="dish_image", @record=#<Dish id: 11, created_at: "2022-04-11 09:28:29", updated_at: "2022-04-11 09:28:29", user_id: 6, dish_name: "エビチリ", introduction: "エニチリです">, @dependent=:purge_later>, #<ActiveStorage::Attached::One:0x000000000394e870 @name="dish_image", @record=#<Dish id: 12, created_at: "2022-04-11 10:41:18", updated_at: "2022-04-11 10:41:18", user_id: 6, dish_name: "エビチリ", introduction: "aaa">, @dependent=:purge_later>] 

pp

先ほどの記述の先頭にppを記述します。
そうすることで表示が見やすくなり大変便利です。

2.6.3 :003 > pp Dish.all.map{|d|d.dish_image}
2.6.3 :003 > pp Dish.all.map{|d|d.dish_image}
  Dish Load (0.1ms)  SELECT "dishes".* FROM "dishes"
[#<ActiveStorage::Attached::One:0x000000000397ec00
  @dependent=:purge_later,
  @name="dish_image",
  @record=
   #<Dish:0x000000000397f3d0
    id: 8,
    created_at: Mon, 11 Apr 2022 08:38:04 UTC +00:00,
    updated_at: Mon, 11 Apr 2022 08:38:04 UTC +00:00,
    user_id: 6,
    dish_name: "エビチリ",
    introduction: "エビチリ">>,
 #<ActiveStorage::Attached::One:0x000000000397eb38
  @dependent=:purge_later,
  @name="dish_image",
  @record=
   #<Dish:0x000000000397f218
    id: 9,
    created_at: Mon, 11 Apr 2022 09:05:20 UTC +00:00,
    updated_at: Mon, 11 Apr 2022 09:05:20 UTC +00:00,
    user_id: 6,
    dish_name: "エビチリ",
    introduction: "qq">>,
 #<ActiveStorage::Attached::One:0x000000000397ea70
  @dependent=:purge_later,
  @name="dish_image",
  @record=
   #<Dish:0x000000000397f0b0
    id: 10,
    created_at: Mon, 11 Apr 2022 09:12:14 UTC +00:00,
    updated_at: Mon, 11 Apr 2022 09:12:14 UTC +00:00,
    user_id: 6,
    dish_name: "エビチリ",
    introduction: "エニチリです">>,
 #<ActiveStorage::Attached::One:0x000000000397e958
  @dependent=:purge_later,
  @name="dish_image",
  @record=
   #<Dish:0x000000000397ef20
    id: 11,
    created_at: Mon, 11 Apr 2022 09:28:29 UTC +00:00,
    updated_at: Mon, 11 Apr 2022 09:28:29 UTC +00:00,
    user_id: 6,
    dish_name: "エビチリ",
    introduction: "エニチリです">>,
 #<ActiveStorage::Attached::One:0x000000000397e7c8
  @dependent=:purge_later,
  @name="dish_image",
  @record=
   #<Dish:0x000000000397ed90
    id: 12,
    created_at: Mon, 11 Apr 2022 10:41:18 UTC +00:00,
    updated_at: Mon, 11 Apr 2022 10:41:18 UTC +00:00,
    user_id: 6,
    dish_name: "エビチリ",
    introducti

間違えている記述あれば教えていただきたいです。
よろしくお願いします。

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