sinatra-activerecord: LoadError: cannot load such file -- active_record/railties/databases.rake

When I require 'sinatra/activerecord/rake'in my Rakefile I get this error

log: https://gist.github.com/anonymous/159f183fb6f46e710379

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 29 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Did you forget to use bundle exec?

You have to run rake -T with bundle exec 😉

$ bundle exec rake -T

You need to run “bundle exec rake db:migrate”, then it should work.

Rake is a task runner, and imagine tasks as simple Ruby scripts that do something. The “-T” flag tells Rake just to print the list of available tasks. See documentation for ActiveRecord on better explanation of these Rake tasks.

Yaaaa…github…I no use…maybe have this comments, some people have same problem. They just try to change this order. 1、require “./app” 2、require “sinatra/activerecord/rake”

maybe ok, no error message. Thank you for janko-m

just upgrade to 2.0.14 , problem solved.

In Ruby there is a thing called “load path” (you can inspect it with puts $LOAD_PATH). This is a list of paths where you require from. Think of it as shell’s PATH variable, where shell searches for executables that you want to run.

When you do require "active_record", it actually requires a file like this:

/Users/janko/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record.rb

So, on it’s own, Ruby just requires the latest version of ActiveRecord, which may not be the version you want.

What Bundler does is “locks” a specific version of ActiveRecord (specified in your Gemfile) by adding the correct version of the gem to the $LOAD_PATH, so then require will require that version of the gem.

Now, you may notice that the $LOAD_PATH doesn’t contain any gems when not using Bundler, yet you can still require gems without Bundler. That’s because require in Ruby is a bit smarter, it knows where are your gems. So, if it isn’t able to require something from the load path, it will search in gem directories.

In this specific case I’m using load instead of require (because I have to require a .rake file, which I can’t do with require). And load is a bit stupider, in the sense that if it doesn’t find the file in the $LOAD_PATH, it won’t search through gems. That’s why you need Bundler, to add ActiveRecord to the load path so that load can find it.