Pass ruby script file to rails console

RubyRuby on-Rails-3

Ruby Problem Overview


Is there a way to pass ruby file, foo.rb to rails console. Expected results would be after console starts rails environment to run file.

Or any other way which would allow me to execute file in rails environment, triggered from command prompt.

Ruby Solutions


Solution 1 - Ruby

Actually, the simplest way is to run it with load inside the rails console

 load './path/to/foo.rb'

Solution 2 - Ruby

You can use

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

UPDATE:

What we also have been using a lot lately is to load the rails environment from within the script itself. Consider doit.rb:

#!/usr/bin/env ruby

require "/path/to/rails_app/config/environment"

# ... do your stuff

This also works if the script or the current working directory are not within the rails app's directory.

Solution 3 - Ruby

In the meantime, this solution has been supported.

rails r PATH_TO_RUBY_FILE

Much simpler now.

Solution 4 - Ruby

script/console --irb=pry < test.rb > test.log

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

Solution 5 - Ruby

Consider creating a rake task.

For code that I need to create records or support migrations, for example, I often create a rake task like that 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
end

And then in the terminal run:

rake example:create_user

Solution 6 - Ruby

Of these approaches mentioned earlier, none seemed clean and ideal like you would expect a standalone script to run (not get eval-ed or piped via < redirection), but finally this works perfect for me:

(for Rails 3)

Insert 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, set your own Rails app path in the APP_PATH line.

That way, I can avoid having to enter any interactive irb or rails c and can test my script.rb from the shell prompt, before eg. scheduling it in crontab.

It smoothly supports command-line parameters, too, and minimizes the levels of wrappers before getting to your code.

CREDIT (also shows a Rails 2 example)

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

Solution 7 - Ruby

Here's the hack I'm using:

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 Rails project root.

I know this is some serious heresy, using python to help a ruby script. But I couldn't find a relpath method built into ruby.

Credit: relpath() was taken from @MestreLion, https://stackoverflow.com/questions/2564634/bash-convert-absolute-path-into-relative-path-given-a-current-directory#comment-12808306

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaris KrajinaView Question on Stackoverflow
Solution 1 - RubyAdrianView Answer on Stackoverflow
Solution 2 - RubymoritzView Answer on Stackoverflow
Solution 3 - RubyHaris KrajinaView Answer on Stackoverflow
Solution 4 - Rubyhari seldonView Answer on Stackoverflow
Solution 5 - RubyMattView Answer on Stackoverflow
Solution 6 - RubyMarcosView Answer on Stackoverflow
Solution 7 - RubyChaim Leib HalbertView Answer on Stackoverflow