I'm trying to manage database.yml using capistrano, following this post:
http://www.simonecarletti.com/blog/2009/06/capistrano-and-database-yml/
I'm running into trouble including the code used in the post above. I've named this file cap_database.rb but I don't where to save it, or how to load it in deploy.rb.
I've tried placing it in lib/capistrano and added it to deploy.rb with this line:
require 'capistrano/cap_database'
and then I get this:
$ cap deploy:db
/home/daniel/.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 -- capistrano/cap_database (LoadError)
Why can't it find the file?
it's just a shot but try
bundle exec cap deploy:db
For recipes at the gem level, put your custom recipes into GEMDIR/CAPGEMDIR/lib/capistrano/recipes
If you want to include additional files into your app's deploy.rb, then you can do it using this method:
require in capistrano deploy.rb cannot find file
I know this is a bit late but here goes anyway.
If you store cap_database.rb in config/recipes, it can be included in deploy.rb using load config/recipes/cap_database.
Related
I am attempting to use the Roo gem to parse a spreadsheet file in my Rails controller. I have added the appropriate line to my Gemfile and ran the bundle command. Here is the line in my controller where everything breaks:
xlsx = Roo::Spreadsheet.open(filepath)
I receive an error: uninitialized constant MyController::Roo. I then added the following line to the top of my controller file (as specified in the Roo readme):
require 'roo'
Now I get a different error: Error during failsafe response: cannot load such file -- roo.
What am I doing wrong?
Did you bundle and restart your server?
Adding gems, modifying app/config files or app/lib files require a server restart :)
In my config\environments\development.rb and config\environments\production.rb files, I set some global variables. In the example below, I have a a Redis instance that points to our cache, and a Statsd instance that points to the DataDog agent.
config.x.cache = Redis.new(url: ENV["CACHE"])
config.x.statsd = Statsd.new('localhost', 8125)
In the case of Redis, I added gem 'redis' to my gem file, ran bundle install and everything worked fine. In the case of StatsD, however, it seems that I need to also add require 'statsd' at the top of the development.rb and production.rb files in order to be able to create the instance. Of course, I also added gem 'dogstatsd-ruby' to my gem file and ran bundle install, but that didn't seem to be enough. If I don't add the require statement at the top of the config files, I get the following error when I try to run my Rails app:
uninitialized constant Statsd (NameError)
Can anyone explain why I have to add the require statement only in this particular case (StatsD), or is there is a better way to do this? Thanks!
I want to read a .yml file included in a Rails Engine Gem from my main_app. The file is located at config/test.yml.
IO.read("config/test.yml") fails with No such file or directory # rb_sysopen
If i move the file into the main app, everything works fine. But i need this file in a Gem.
Solution 1
You can store the root path in a constant from your main gem file and retrieve it in other locations of your code. You must ensure that the gem got initialized before the code in your app runs otherwise you'll have an errors because the constant won't be defined.
# lib/my_gem.rb
GEM_ROOT = File.expand_path("../..", __FILE__)
# app/.../some_class.rb
IO.read("#{GEM_ROOT}/config/test.yml")
Solution 2
The most advisable, you can get the gem path programmatically from Bundler, then use that root path to retrieve the full path of your yml file.
Have a look at this answer that you can easily adapt to your case
You should be able to load app yaml files by doing this in your gem:
YAML.load_file('config/test.yml')
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.
I'm trying to use collectiveidea's delayed_job gem
The installation instructions include
Rake tasks are not automatically loaded from gems, so you’ll need to add
the following to your Rakefile:
begin
require 'delayed/tasks'
rescue LoadError
STDERR.puts "Run `rake gems:install` to install delayed_job"
end
Where is my Rakefile? And what is a Rakefile?
I had the same problem with rails 3.1 and collectiveidea-delayed_job.
Once I added Delayed::Worker.backend = :active_record in the initializer I got the error
no such file to load -- delayed/backend/active_record (LoadError)
The solution for me was to add gem 'delayed_job_active_record' in the gemfile, as suggested here
I have the same problem and put that code in delayed_job.rake in the lib/tasks directory. It works, but now It say's:
*** Starting job worker localhost pid:79949
rake aborted!
uninitialized constant Delayed::Job
What is wrong now?
UPDATE: I just got a mail answer from Brandon:
Theres a bug in the latest version where it doesn't get properly initialized when using the rake task. If you create a file in config/initializers and put the follow in it, the error should go away:
Delayed::Worker.backend = :active_record
The Rakefile is a file that is used to configure rake, a Ruby build tool (sort of like make, but all in Ruby). In a Rails project, there is a file in the top project directory named Rakefile where you can insert this code.
Alternatively, you can add a file into the lib/tasks directory (for example named delayed_job.rake) and put the code into there. The name of the file is not important as long as
It is in the lib/tasks directory
It has the extension .rake