11 Apr
Rails Tricks Issue 2: Console Tricks
Hi, this is Greg with the second issue of Rails Tricks!
Thanks to everyone for subscribing!
This week, I want to share a few Rails console tricks I use regularly. The first one is the _ command, which always holds the return value of the last evaluated command in IRB, which is the default for Rails` console. For instance, if you run an Active Record query to find a record, then you realize that you want to keep the result around, you can easily assign it to a variable:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTgsInB1ciI6ImJsb2JfaWQifX0=--419702f7e446c56b05c4cd42632aa720ec1981fd/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/39f85102-9d90-419a-9421-092d9fdd535a.png)
Another thing I regularly do is, to look up the definition of methods or the location of their definition. This comes in handy when you are debugging an issue and can be helpful to see what a method actually does or where it is defined.
Because maybe a gem overrides it, and that causes unexpected behavior. In a Rails console, to see the definition, you can use Object.method(:name).source:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTksInB1ciI6ImJsb2JfaWQifX0=--2b3f25daece19c0589af7d70b1346b7b08459d13/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/3a342bd2-e713-41a2-b113-22ef6f36dd50.png)
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NjAsInB1ciI6ImJsb2JfaWQifX0=--34c1e7cb22b7be88c868320a3dc01b45f3fafe8e/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/e0c5a374-e943-44e7-ae68-1722e434bc56.png)
The next thing I want to share is the --sandbox option. If you pass this option when you start your console, every database modification will be rolled back on exit.
This can be useful when you are debugging something in production, and you want to make sure you are not messing up something accidentally.
And the final thing is to have some fun! Type IRB.send(:easter_egg, :dancing) or IRB.send(:easter_egg, :logo) in your IRB session and enjoy the result!
You can exit by pressing CTRL+C.
P.S.: I just came across this blog post recently from Jemma about the measure command in IRB: https://jemma.dev/blog/irb-measure.
This week I have a special guest trick:
Hello there, I'm Adrian, the author of Avo, and I'm excited to share a neat trick with you today. In this tutorial, I'll be demonstrating how to attach hooks and business logic to Avo's controllers using a Current model.
You may already be familiar with Rails' Current model, which is used to set the current user, multi-tenancy accounts, or other pieces of information. Typically, before_action is used in your ApplicationController to demonstrate how Current works. However, if you attempt to apply the same changes to your app the action will not work in Avo's context. This is because Avo has its own AplicationController.
So how do we apply this behavior inside Avo? Let me walk you through it.
Firstly, we configure the Current model and create the concern that holds the business logic.
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NjEsInB1ciI6ImJsb2JfaWQifX0=--2454d49554774b2c5e0a38dcc545a95b9a7ae015/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/4d4a88e0-a863-4a4c-af1d-cfb7581957a9.png)
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NjIsInB1ciI6ImJsb2JfaWQifX0=--be067a98daf581a1bde5269c769c2d1aefcff275/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/29c47f32-3ebd-4107-a264-30c5acf7ddee.png)
This technique can also be applied to other classes, such as including all helpers inside Avo's field declarations:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NjMsInB1ciI6ImJsb2JfaWQifX0=--4967ad06dce01b4624abd381ccd4a15d7d133c9a/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/ff4fdf89-8e21-4b8a-aeab-52489c8747d2.png)
Adrian ✌
That’s it for this week, see you next time!
Greg