Arunachalam
Arunachalam

Reputation: 6037

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?

Upvotes: 3

Views: 1374

Answers (2)

Jeremy Baker
Jeremy Baker

Reputation: 4272

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

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160551

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.

Upvotes: 7

Related Questions