I have a ruby script I wrote which generates data and loads it to MongoDB. I am now trying to call this load script from seed.rb of my Rails app (so I can run via rake db:seed)
I attempted to call it from rails using this code:
system( "ruby data_load/db_load.rb -a data_load/doc1.json" )
When that code executes, I get the following error. (Note it runs fine from the command line):
data_load/db_load.rb:15:in `require': cannot load such file -- mongo (LoadError)
from data_load/db_load.rb:15:in `<main>'
The top of db_load.rb looks like this:
# includes gems from gemfile
require 'rubygems'
require 'bundler/setup'
Bundler.setup
require 'mongo'
require_relative 'load_scripts/cmd_options'
require_relative 'load_scripts/build_index'
....
include Mongo
The script has it's own gemfile in the data_load directory.
My guess is ruby is running the script using the bundle for the rails application instead of the shell script.
Any suggestions on how I can execute this script?
I believe the problem is where Bundler is looking for the Gemfile. Since your script is being run in the parent directly it is finding the Gemfile for the main app.
Set the BUNDLE_GEMFILE before calling your script:
system "BUNDLE_GEMFILE=data_load/Gemfile ruby data_load/db_load.rb -a data_load/doc1.json"
I'm sorry but I think you can't do it, unless you run the script as a different process (like a shell command), doing it is easy:
`shell_command params`
Just use the correct path and params.
Otherwise, consider that a gemfile is "more or less" at its basic level, a bunch of require (or load) statements, so loading a different gemfile would overwrite the original one, creating a lot of issue with rails after that.
The subprocess command is a good idea, however you can only pass string as params, not in-memory objects.
Related
Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.
I have a ruby on rails api_app and and a test_app that exercises the api_app. While development mode I want to run, from the api_app, a rake task called match:reset that is in the test_app. I am trying to do this with a ruby file that is in the api_app
#!/usr/bin/env ruby
require 'tty-command'
cmd = TTY::Command.new
Dir.chdir('../test_app') do
cmd.run 'run rake match:reset'
end
When I do this I get the following error
Your Ruby version is 2.4.4, but your Gemfile specified 2.4.1 (Bundler::RubyVersionMismatch)
I have tried adding cmd.run 'rvm use 2.4.1' but this does not work. How do set the correct environment so this does work?
I could not get this working with a ruby command file. I think the reason for this is that the file is run with ruby from the api_ppp directory so you cannot change to another ruby. What I ended up doing is using a bash file:
#!/bin/bash
cd ../test_app
source $HOME/.rvm/scripts/rvm
rvm use 2.4.4#test_app_2.4.4
BUNDLE_GEMFILE=../test_lab/Gemfile bundle exec rake -f ../test_app/Rakefile match:reset
I am happy to consider an answer that uses a ruby command file, as long as the answer has been tested.
This solution is not tested, based on the documentation I have made changes to the script.
require 'tty-command'
require 'rake'
cmd = TTY::Command.new
cmd.run("rvm use 2.4.1")
Dir.chdir("../test_app") do
cmd.run(:rake, 'match:reset', env: {rails_env: :development})
end
I've a watir script that I want to fire off from a Rails app on demand. My controller action looks something like this:
def run_scrape
pid = Process.spawn('C:\Ruby22-x64\bin\ruby.exe "C:\Users\Admin\Dropbox\dev\watir-scripts\ds-vin.rb" 3VWB07AJ')
Process.detach(pid)
true
end
The actual script starts with:
require 'watir-webdriver'
b = Watir::Browser.new :ie
# etc
Now, if I navigate to the folder and just run the script directly, everything works fine. Once it's triggered from Rails (which is at a different location; seems like that's messing it up) I'm greeted with the following error:
`require': cannot load such file -- watir-webdriver (LoadError)
Watir-webdriver is in my gem list. I'm also requiring a couple of .rb files in the same folder, which was a problem until I used require_relative. I'm missing something simple here, I think. How do I require_relative a global gem?
First solution is to run your scraper in the same process as Rails, but in a different thread:
require 'C:\Users\Admin\Dropbox\dev\watir-scripts\ds-vin.rb'
thr = Thread.new do
b = Watir::Browser.new :ie
...
end
Second is to fix your environment if you are going to use Process.spawn - assuming your gems are installed system-wide you can do something like
ENV['GEM_PATH'] = "c:/Ruby220/lib/ruby/gems/2.2.0"
at the top of your ds-vin.rb script. To find the exact gem path you should use, make sure you run gem which watir-webdriver and gem environment and copy from there.
We have a mail gem installed in our vendor/cache directory inside a Rails application.
The script is called "test" and is not inside the Rails application directory.
#! /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby
require 'date'
require 'fileutils'
require 'openssl'
require 'yaml'
require 'mail'
require 'dalli'
I get the following error when I execute this script from outside the Rails application.
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mail (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
I simply re-installed these gems (mail, dalli) in the standard Ruby path and it worked, but that's not ideal.
What do we need to so that these installed gems are found when we try to run this script outside of a Rails app? In other words, how do we specify the path to these gems?
Be sure that your gem are all declared in your Gemfile:
gem 'mail'
If you don't want them to be loaded by default, and load them only when needed, you can use the require statement that you are already using, and in your Gemfile add :require => false:
gem 'mail', :require => false
When you call your script from outside your Rails environment, and want to load the gems, prefix your script by bundle exec:
bundle exec my_script.rb
If you need to run your script from another location than your rails's app root, you must run:
BUNDLE_GEMFILE=/path/to/your/app/Gemfile bundle exec your_script
Keep in mind though that this may cause path issues if your script or your gems are looking for file in the path of your rails app
I'm trying to run individual tests through ruby test/unit/mytest.rb, but I always get a "no such file to load - test_helper" error. Google brought up a few suggestions, but none of them worked for me. I'm running Rails 3.0, Ruby 1.9.2 (through RVM) on Ubuntu 10.10
Here's what I've tried so far - any suggestions really appreciated
Changed the "require test_helper" to "require File.dirname(FILE) + "/../test_helper"
" in test/unit/mytest_test.rb. It brings back " no such file to load -- test/unit/../test_helper"
Tried running rvm test/unit/mytest_test.rb Same as above
Tried running ruby -I test/unit/mytest_test.rb. No messages to the terminal. After about 5 minutes waiting for something to happen, ctrl+c'd out of it
Any suggestions very appreciated - I'm stumped.
ruby 1.9.2 removed ".", the current directory, from the load path. I have to do this to get it to work:
require 'test_helper'
and call it like:
ruby -I. unit/person_test.rb
I was fighting this thing myself today and i dislike the big require with whole path to file and stuff...
In my case it was fault of Rakefile..
so now it looks like this:
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "lib"
t.libs << "test" # here is the test_helper
t.pattern = "test/**/*_test.rb"
end
task default: :test
I know its old and has answer marked accepted, but maybe this will also help someone :)
have a nice day
I've added the following to the top of my test files.
require File.expand_path("../../test_helper", __FILE__)
This restores the previous behavior and allows the call to be simply:
ruby test/unit/person_test.rb
Maybe you should run your test cases in this way:
$ rake test
There is no need to change the "require" statement from generated code if you use rake.
Tested with Ruby 1.9.3 and Rails 3.2.8
If you are creating a gem or engine, running rake test in the test dummy application directory will cause this error. Running rake test in the root of the gem will avoid this.
Rails 1.9 no longer includes the current directory in the LOAD_PATH, which causes this problem. You have a few options.
call the test with the -I option from the app dir:
ruby -I test test/functional/test_foo.rb
and use a require with no path:
require "test_helper.rb"
use a full path in the require. Either
require 'pathname'
require Pathname.new(FILE).realpath.dirname.join('/../test_helper.rb')
or
require (File.dirname(File.realdirpath(__FILE__)) + '/../test_helper.rb')