I’ve embedded (or at least I think I’ve embedded) the rails models from my acme app into my Sinatra app using the following code, based off of a sample I had found on someone’s blog. The rails app is in /Users/chris/acme and the Sinatra app is in /Users/chris/acme/services/sinatra/sinatra.rb.
RAILS_ROOT = '../..'
LIB_DIR = "#{RAILS_ROOT}/lib"
MODELS_DIR = "#{RAILS_ROOT}/app/models"
require 'active_support/dependencies'
ActiveSupport::Dependencies.autoload_paths += Dir["#{LIB_DIR}/**/"]
ActiveSupport::Dependencies.autoload_paths += Dir["#{MODELS_DIR}/"]
require 'active_support/all'
require 'active_record'
::ActiveRecord::Base.establish_connection(
YAML.load(File.read(
File.expand_path('config/database.yml', RAILS_ROOT)))["development"])
require 'sinatra'
get '/user' do
"#{User.first.id}"
end
When I visit http://localhost:4567/user I get the following error:
LoadError - no such file to load -- util/versioning:
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `require'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `block in require'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:597:in `new_constants_in'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `require'
/Users/chris/acme/app/models/user.rb:2:in `<top (required)>'
Line 2 of user.rb is require 'util/versioning', which is something that should have been autoloaded, but wasn’t.
If I change line 2 of user.rb to require '/Users/chris/acme/lib/util/versioning' then everything starts working, but there’s got to be a better way, right?
I removed
require 'util/versioning'fromuser.rband now it’s working fine, including calls toUtil.Versioningfrom withinuser.rb. This works with either a relative or an absoluteRAILS_ROOT. I guess I seem to be misunderstanding how to userequireproperly as I’m not sure why I don’t need to require a library that I’m definitely using inuser.rb.