Skip ruby ​​script file to rails console

Is there a way to transfer the ruby ​​file, foo.rb to the rails console. The expected results are that the console starts the rails environment to run the file.

Or in any other way that would allow me to execute the file in the rails environment, called from the command line.

+77
ruby-on-rails-3
Apr 25 2018-12-12T00:
source share
7 answers

In the meantime, this decision was supported.

rails r PATH_TO_RUBY_FILE 

Much easier.

+17
Jun 16 '16 at 12:10
source share

Actually, the easiest way is to run it using load inside the rails console

  load './path/to/foo.rb' 
+120
Nov 02 '13 at 23:52
source share

you can use

 bundle exec rails runner "eval(File.read 'your_script.rb')" 

UPDATE:

What we also used recently is to load the rails environment from the script itself. Consider doit.rb :

 #!/usr/bin/env ruby require "/path/to/rails_app/config/environment" # ... do your stuff 
+72
Apr 25 2018-12-12T00:
source share
 script/console --irb=pry < test.rb > test.log 

simple, dirty and block the process at the end, but it does the job exactly the way I wanted.

+4
Feb 03 '13 at 15:29
source share

Of the approaches mentioned earlier, none of them looked clean and perfect, as you would expect a stand-alone script to run (not get an estimate or pass through <redirection), but in the end, this works great for me:

(for Rails 3)

Paste at the top of your script:

 #!/usr/bin/env ruby APP_PATH = File.expand_path(appdir = '/srv/staging/strat/fundmgr/config/application', __FILE__) require File.expand_path(appdir + '/../boot', __FILE__) require APP_PATH # set Rails.env here if desired Rails.application.require_environment! # your code here... 

Of course, configure your own Rails path in the APP_PATH line.

That way I can avoid typing any interactive irb or rails c and can check my script.rb from the command line before, for example. planning it in crontab.

It smoothly supports command line options and minimizes wrapper levels before moving on to your code.

CREDIT (also shows an example of Rails 2)

http://zerowidth.com/2011/03/18/standalone-script-runner-bin-scripts-in-rails.html

+1
Oct 27 '14 at 15:56
source share

Here I use hack:

 rr() { rails_root="$(bundle exec rails runner "puts Rails.root")" rp="$(relpath "$1" "$rails_root")" bundle exec rails runner "eval(File.read '$rp')" } relpath() {python -c "import os.path; print os.path.relpath('$1','${2:-$PWD}')";} 

Example:

 cd ~/rails_project/app/helpers rr my_script.rb 

Based on @moritz's answer here. I changed it since the working directory for File.read is the root of the Rails project.

I know this is a serious heresy using python to help a ruby ​​script. But I could not find the relpath method built into ruby.

Credit: relpath() was taken from @MestreLion, Convert absolute path to relative path given current directory using Bash

0
Jun 29 '15 at 10:14
source share

Consider creating a rake task.

For the code I need to create records or support migration, for example, I often create a similar task from this answer . For example:

In lib/tasks/example.rake :

 namespace :example do desc "Sample description you'd see if you ran: 'rake --tasks' in the terminal" task create_user: :environment do User.create! first_name: "Foo", last_name: "Bar" end 

And then in terminal run:

 rake example:create_user 
0
Feb 28 '17 at 1:41
source share



All Articles