[英]Error loading Active Record gem with sinatra app using RVM
我为我正在启动的sinatra应用程序设置了一个项目级别的RVM gemset,它将使用Active Record连接到本地数据库。 为了测试它,我尝试运行以下测试应用程序:
test.rb
require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'activerecord'
class Article < ActiveRecord::Base
end
get '/' do
Test.establish_connection(
:adapter => "sqlite3",
:database => "hw.db"
)
Test.first.content
end
(摘自这个问题的答案: 使用Sinatra时与数据库交谈的最佳方式是什么? )
当我运行ruby -rubygems test.rb
此错误:
/Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- activerecord (LoadError)
我已经安装了Active Record gem,它显示在gem list
, rvm current
显示正确的gemset。 我是RVM的新手,我认为这与它没有正确的加载路径有关,但我觉得我已经正确设置了所有内容,所以我很欣赏有关错误的建议。 谢谢。
据我所知,'activerecord'已被弃用。 尝试使用
require 'active_record'
代替。
如果您尚未安装activerecord gem,则还会收到该错误:
打开命令提示符并在终端中运行以下命令:
#Find if the active record gem is already installed on your computer:
gem query --local
#See the downloadable gems, and see if activerecord is still available:
gem query --remote --name-matches activerecord
#Install your gem:
gem install --remote activerecord
#See if it installed successfully and is in the installed gem list:
gem query --local
下面是一些使用ActiveRecord gem查看是否所有内容都配置正确的代码:
#Ruby code
require 'active_record'
class Dog < ActiveRecord::Base
has_many :dog_tags
end
puts "activerecord gem is installed";
如果一切正常,它将打印“activerecord gem is installed”,没有任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.