Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run some default commands after firing the Rails console?

I need to run a few commands whenever I start my Rails console, like setting up the logger, or to set the time to my time zone.

Right now I'm copying and pasting these commands after Rails is started. Can I write a script to make these commands run automatically after IRB is started?

like image 310
Arunachalam Avatar asked Sep 20 '25 02:09

Arunachalam


2 Answers

Rails' console is IRB. IRB supports an .irbrc file, which contains initialization information and settings.

Read "https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick" and "My .irbrc for console/irb" for ideas.

like image 188
the Tin Man Avatar answered Sep 22 '25 14:09

the Tin Man


I wrote an extended answer to this in another question but the short answer is that if you are using Rails 3 or above you can use the console method on YourApp::Application to make it happen:

module YourApp
  class Application < Rails::Application
    ...

    console do
      Time.zone = A.info.time_zone
    end
  end
end
like image 33
Jeremy Baker Avatar answered Sep 22 '25 14:09

Jeremy Baker