Boost Productivity with Custom Console Helpers in Rails
Make the Rails console more efficient by defining custom methods that load automatically. This is useful for repetitive tasks or frequently accessed records.
Example: Fetching a Test Product
If you often need to work with a specific product for testing, you can add a helper method to simplify your workflow.
Create the following initializer:
Rails.application.console do
def test_product
Product.find_by(name: 'Sample Product')
end
def send_test_email
ActionMailer::Base.mail(
from: "admin@example.co",
to: "catch-mail@example.com",
subject: "Test",
body: "Test"
).deliver_now
end
end
When you open the console, you can call test_product
to quickly retrieve the desired product.
Keep It Personal
If this setup is meant only for your local environment, add the initializer file to .gitignore
. This prevents it from being shared with other developers in the project.