I have a /scripts folder that contain some ruby scripts for my project.
/scripts
/scripts/app/models.rb
/scripts/config/config.yml
/scripts/lib
/scripts/lib/myapp/test.rb
In my test.rb, I have the following code which correctly loads the config.yml file:
env = ENV['ENV'] || 'development'
config = YAML::load(File.open('config/config.yml'))[env]
Now at the top of the test.rb file, I am trying to load my models.rb file which I am using with ActiveRecord, but I get an error:
require File.join(File.dirname(__FILE__), "../../", 'models')
Error:
1.9.1/rubygems/custom_require.rb:36:in `require’: cannot load such file — lib/myapp/../../models (LoadError)
I tried:
require 'app/models'
that didn’t work either.
What am I doing wrong, and why does the config.yml file load correctly?
File.open uses explicit path, but require tries to search in PATH. Ruby >= 1.9.2 does not add current directory to a search path – so you need to specify it explicitly
or use require_relative
Alternatively you can add current directory to search path (not recommended, this was removed from ruby for security considerations)