Search code examples
ruby-on-railsrubyirb

How to run script before every Rails console invocation?


I'm pretty tired of writing this line every time I want to open the Rails console:

irb(main):001:0> ActsAsTenant.current_tenant = User.find(1).account

Is there any way to run command/script before every "rails c"/"irb" invocation?

Thanks in advance!


Solution

  • Put the code you want to execute into .irbrc file in the root folder of your project:

    echo 'ActsAsTenant.current_tenant = User.find(1).account' >> .irbrc
    bundle exec rails c # ⇐ the code in .irbrc got executed
    

    Sidenote: Use Pry instead of silly IRB. Try it and you’ll never roll back.