ruby 未初始化的常量 ActiveRecord (NameError)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15731068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uninitialized constant ActiveRecord (NameError)
提问by Jonathan Kav
I am running a script (that a friend I lost touch with wrote for me.) It starts like this:
我正在运行一个脚本(我失去联系的一个朋友为我写的。)它开始是这样的:
require 'ruby-debug'
require 'circle'
first_circle=Circle.new()
@number_of_rounds=1
But I keep getting this error message:
但我不断收到此错误消息:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle/models/friendship.rb:1:in
`<top (required)>': uninitialized constant ActiveRecord (NameError)
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
from
C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle/circle.rb:1:in
`<top (required)>'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
from
C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle.rb:7:in
`<top (required)>'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in
`require'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in
`rescue in require'
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in
`require'
from primes.rb:5:in `<main>'
What should I do?
我该怎么办?
回答by Joshua Cheek
This was cross-posted to ruby-talk.
这是交叉发布到ruby-talk。
ActiveRecord is a class that talks to databases, this gem is expecting to be run in a context with a database connection to ActiveRecord loaded. If you're in Rails, that means loading your Rails environment. Or if just ActiveRecord, something like this will work:
ActiveRecord 是一个与数据库对话的类,这个 gem 期望在加载了 ActiveRecord 的数据库连接的上下文中运行。如果您使用 Rails,这意味着加载您的 Rails 环境。或者,如果只是 ActiveRecord,这样的事情将起作用:
require 'active_record'
require 'circle'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table :users do |t|
t.string :name
t.integer :friends_count, :default => 0, :null => false
end
create_table :friendships, :force => true do |t|
t.references :user, :friend
t.datetime :requested_at, :accepted_at, :denied_at, :blocked_at
t.string :status
t.timestamps
end
create_table :blocked_users, :force => true do |t|
t.references :user, :blocked_user
t.timestamps
end
change_table :friendships do |t|
t.index :user_id
t.index :friend_id
t.index :status
end
change_table :blocked_users do |t|
t.index :user_id
t.index :blocked_user_id
end
end
class User < ActiveRecord::Base
has_circle
end
john = User.create! name: 'john'
mary = User.create! name: 'mary'
paul = User.create! name: 'paul'
john.befriend(mary)
john.friends?(mary) # => false
mary.accept_friend_request(john)
mary.friends?(john) # => true
But to be honest, if you don't know what ActiveRecord is, then it seems improbable that this gem will be solving problems for you. Also, I'd be a bit skeptical of this gem, it has a misspelling in its migrationsuch that it doesn't actually work unless you go fix it. This has been broken for at least 7 months without being fixed. There's < 800 downloads of the gem, which is not many (few users = fewer people finding and fixing bugs), and it doesn't look like the author is intending to maintain it.
但老实说,如果您不知道 ActiveRecord 是什么,那么这个 gem 似乎不太可能为您解决问题。另外,我对这个 gem 有点怀疑,它在迁移过程中拼写错误,因此除非你去修复它,否则它实际上不起作用。这已经被破坏了至少 7 个月而没有得到修复。gem 的下载量小于 800,这并不多(用户少 = 发现和修复错误的人更少),而且看起来作者并不打算维护它。
Okay, I just realized what is actually happening. Took about 20 min to write that up above, and it might help someone later googling for an issue, so I'm going to leave it. What really happened, I suspect, is that you have a gem on your system named circle, and you have a file probably in your same directory named circle. Your load path is not properly set, so when you require 'circle', it is finding the gem and not the file you wrote. A simple answer is to say require File.dirname(__FILE__) + '/circle'instead of require 'circle'This isn't really the right answer, but it will work without going into the myriad of nuances required to figure out what the right thing is. If you want to figure out what the right thing is, I'd need to know what Ruby version you're on, how you're intending to use and invoke this code, and what your directory structure looks like.
好吧,我刚刚意识到实际发生了什么。花了大约 20 分钟把它写在上面,它可能会帮助某人以后在谷歌上搜索问题,所以我要离开它。我怀疑,真正发生的事情是您的系统上有一个名为 circle 的 gem,并且您可能在同一目录中有一个名为 circle.gem 的文件。您的加载路径设置不正确,因此当您require 'circle'找到 gem 而不是您编写的文件时。一个简单的答案是说require File.dirname(__FILE__) + '/circle'而不是require 'circle'这不是真正的正确答案,但它会起作用,而无需涉及找出正确事物所需的无数细微差别。如果您想弄清楚什么是正确的,我需要知道您使用的是哪个 Ruby 版本,您打算如何使用和调用此代码,以及您的目录结构是什么样的。
Also. If you would have said that circle.rb was a file in the same directory, then I wouldn't have lost 20 min with the top answer. You should provide sufficient context in the future to understand the problem.
还。如果你说 circle.rb 是同一目录中的一个文件,那么我不会因为最佳答案而失去 20 分钟。您应该在将来提供足够的上下文来理解问题。
回答by wacha
Since this page is the first to come out when googling the error message, it might help to note that this error message can arrise when trying to run rspecon app/models/...instead of spec/models/...
由于此页面是第一个走出来使用Google的错误消息时,它可能会帮助需要注意的是此错误消息可以arrise试图运行时的RSpec上app/models/...,而不是spec/models/...
回答by Aleksei Matiushkin
You need to
你需要
gem install activerecord
Hope this helps.
希望这可以帮助。

