0

I'm trying to run shell command with rake task of another project in rails controller:

def generate
  render json: `cd /Users/user/project && rvm use ruby-2.1.1@gemset && bundle exec rake users:build`
end

The problem is that not working for that task (nothing happening) but if I run rake routes for example it is running well.

1
  • @downvoter, how should I edit my question? Commented Feb 13, 2015 at 14:42

2 Answers 2

1

if you are running another ruby project, make sure you clean the current bundler environment before run command. Bundler provides an easy way to tackle this inconvenient by doing:

 Bundler.with_clean_env{   
    render json:  `cd /Users/user/project  && rvm use ruby-2.1.1@gemset && BUNDLE_GEMFILE=path/to/Gemfile bundle exec rake users:build`
 }

take note of BUNDLE_GEMFILE and Bundler.with_clean_env

Best!

Sign up to request clarification or add additional context in comments.

4 Comments

seems it's not working - Bundler.with_clean_env I need to run bundle install because gems not found
are you using rvm or rbenv ? maybe you have to set the proper ruby version to run that command on that app
I'm using rvm. I've updated my question - added choosing a ruby version with right gemset, but it's still not working - nothing happening
set BUNDLE_GEMFILE option point to the Gemfile file. I've updated the example code
0

Instead of issuing a system call, can you try this (place the following snippet in your controller file):

require 'rake'

Rake::Task["users:build"]

3 Comments

is it possible to run rake task which is from another project?
@PavelBabin, since rake files are essentially ruby files, create a blank rake task file in your current project with the same name (as for users:build) and simply include the eternal rake task file in your rake task file.
thanks for advice, but I need to run a rake task from directory of other project - this is the main problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.