How to require a console only file

I have a Ruby class that I will use only for the console, these are the monkeys of the ActiveRecord::Base patches with some shortcuts, such as ua for update_attribute , and I do not want to load it when running the rails server , but only when the rails console command is run.

What is the way to achieve this?

+5
source share
2 answers

rails console defines Rails::Console

So you can do

 if defined?(Rails::Console) # this runs only in rails console end 

Another approach would be to use config/application.rb :

 module MyApplication class Application < Rails::Application console do require 'my_console_file' end end end 
+5
source

You can configure the new โ€œconsoleโ€ environment by copying one of the existing ones and make the necessary changes, and then always launch the console in this environment. It looks like he will reduce the occasional side syndrome.

+1
source

Source: https://habr.com/ru/post/1242610/


All Articles