Haris Krajina
Haris Krajina

Reputation: 15266

Pass ruby script file to rails console

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.

Upvotes: 146

Views: 84991

Answers (7)

Matt
Matt

Reputation: 6320

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

Upvotes: 6

moritz
moritz

Reputation: 25757

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.

Upvotes: 98

Haris Krajina
Haris Krajina

Reputation: 15266

In the meantime, this solution has been supported.

rails r PATH_TO_RUBY_FILE

Much simpler now.

Upvotes: 98

Chaim Leib Halbert
Chaim Leib Halbert

Reputation: 2324

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, Convert absolute path into relative path given a current directory using Bash

Upvotes: 0

Marcos
Marcos

Reputation: 4920

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

Upvotes: 1

Adrian
Adrian

Reputation: 9590

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

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

Upvotes: 202

hari seldon
hari seldon

Reputation: 51

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.

Upvotes: 5

Related Questions