How to easily get the last value returned in Rails console
Published on 2024-10-09
In rails console
we can get the last returned value by using an _
.
Let’s imagine we’ve opened rails console and done the following:
example_app(dev)> Note.first
Note Load (3.8ms) SELECT "notes".* FROM "notes" ORDER BY
"notes"."id" ASC LIMIT 1
=>
#<Note:0x0000000120078408
id: 1,
created_at: "2024-10-04 22:27:19.177393000 +0000",
updated_at: "2024-10-04 22:27:19.177393000 +0000",
title: "Note 1",
description: "Stuff">
example_app(dev)>
Great, we’ve got the first note, but we forgot to assign that that to a variable so we can reuse it. Easy fix!
example_app(dev)> note = _
=>
#<Note:0x0000000120078408
...
example_app(dev)>
We can now see that Note.first has been saved to our note variable:
example_app(dev)> note
=>
#<Note:0x0000000120078408
id: 1,
created_at: "2024-10-04 22:27:19.177393000 +0000",
updated_at: "2024-10-04 22:27:19.177393000 +0000",
title: "Note 1",
description: "Stuff">
example_app(dev)>