How to run a script before each call to the Rails console?

I really like to write this line every time I want to open the Rails console:

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

Is there a way to run the / script command before each call to "rails c" / "irb"?

Thanks in advance!

+4
source share
2 answers

Put the code you want to execute in the file .irbrcin 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 Pryinstead of stupidity IRB. Try it and you will never give up.

+2
source

You can put your installation code in an rb file, for example:

foo.rb:

def irb_setup
    ActsAsTenant.current_tenant = User.find(1).account
end

run irb as follows:

irb -r ./foo.rb 

( )

2.3.0 :001 > init_irb

, , , - , , . , .

+1

All Articles