未初始化的常量ActiveRecord :: ConnectionAdapters :: ConnectionManagement

时间:2016-07-01 06:00:04

标签: ruby postgresql activerecord sinatra

我目前正在研究sinatra应用程序,并且我在与sinatra的postgresql连接方面遇到麻烦,我尝试执行此命令:

  

rake db:create

创建数据库,但会抛出此错误。

C:\Users\John\Documents\Registration_Sinatra>rake db:create
rake aborted!
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
LoadError: cannot load such file -- sinatra/activerecord
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
(See full trace by running task with --trace)

这是我的app.rb

require 'sinatra'
require 'sinatra/activerecord'
require 'pg'
require './config/environments'
class RegistrationSinatra < ActiveRecord::Base
end 
get '/' do
erb :index
end

这是我的environment.rb

configure :development do
@DEFAULT_CONN = {database: 'development_registration_sinatra', user: 'postgres', password: 'secret123', host: 'localhost'}
db = URI.parse(ENV['DATABASE_URL'] || "postgres://#{@DEFAULT_CONN[:host]}/#{@DEFAULT_CONN[:database]}?user=#{@DEFAULT_CONN[:user]}")
ActiveRecord::Base.establish_connection(
:adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host     => @DEFAULT_CONN[:host],
:username => @DEFAULT_CONN[:user],
:password => @DEFAULT_CONN[:password],
:database => db.path[1..-1],
:encoding => 'utf8')
end

这是我的宝石文件

source 'https://rubygems.org'
ruby "2.2.2"
gem 'sinatra'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'tux'
gem 'pg'

和我的Rakefile

require './app/app'
require 'sinatra/activerecord/rake'

希望你们能够指出我的示例应用程序出了什么问题,以便我可以继续感谢。

1 个答案:

答案 0 :(得分:4)

以下是解决方案:https://github.com/janko-m/sinatra-activerecord/pull/66

在你的Gemfile中,添加:

gem "activerecord", "< 5.0.0"

运行bundle update,它会起作用。