service-oriented-design-with-ruby
service-oriented-design-with-ruby copied to clipboard
Book p19: ruby service.rb -p 3000 -e test
Hi Paul,
After getting the my previous issue to work (yay), the next command on p19 is to start the Sinatra test environment:
ruby service.rb -p 3000 -e test
Every time it gave me an error of 'unknown adapter' or something (forgive my unspecificity here). Going back and forth and a lot of googling, I finally managed to narrow it down to ARGV. In service.rb, this sets up the environment:
# setting up our environment
env_arg = ARGV.index("-e")
env = env_arg || ENV["SINATRA_ENV"] || "development"
databases = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection(databases[env])
Please correct me if I'm wrong, but my limited knowledge of Ruby tells me (or actually ruby-lang.org does) that the line
env_arg = ARGV.index("-e")
returns an integer value. Then the line
ActiveRecord::Base.establish_connection(databases[env])
cannot connect to the database, as it is expecting a string. I adjusted the culprit to
env_arg = ARGV[ARGV.index("-e") + 1]
and now everything seems to be working copacetic.
I'd love to know if this is the right workaround, or that I should change something else.
Rgdz, Oscar
P.S. after having adjusted the file as above, it gives me another error... if I change the line back to the original, then it works as expected:
env_arg = ARGV.index("-e")
with
ruby service.rb -p 3000 SINATRA_ENV=test