Ruby on Rails NameError:未初始化的常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4310161/
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
Ruby on Rails NameError: uninitialized constant
提问by Brian
I just set up a new migration and model relationships, and in console when testing the relationship between tables I get the following error: NameError: uninitialized constant.
我刚刚设置了一个新的迁移和模型关系,并且在控制台中测试表之间的关系时出现以下错误:NameError:未初始化的常量。
Does anyone have any idea what is wrong?
有谁知道出了什么问题?
Thank you
谢谢
Edit:
编辑:
Here's the error
这是错误
NameError: uninitialized constant Profile::ProfileNotification
from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in `const_missing'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2199:in `compute_type'
from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2195:in `compute_type'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `send'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `klass'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:187:in `quoted_table_name'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_many_association.rb:97:in `construct_sql'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:21:in `initialize'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `new'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `profile_notifications'
from (irb):3
Code from the ProfileNotification migration:
ProfileNotification 迁移中的代码:
class CreateProfileNotifications < ActiveRecord::Migration
def self.up
create_table :profile_notifications do |t|
t.integer :profile_id, :null => false
t.integer :notification_id, :null => false
t.string :notification_text
t.boolean :checked, :default => false
t.boolean :update_reply, :default => false
t.boolean :opinion_reply, :default => false
t.boolean :message_reply, :default => false
t.boolean :pm, :default => false
t.boolean :accepted_friend, :default => false
t.boolean :accepted_supporter, :default => false
t.timestamps
end
end
def self.down
drop_table :profile_notifications
end
end
回答by Brian
Well, I figured out the problem. When I was running ruby script/generate model, I was typing ruby script/generate model ProfileNotifications. When I typed ruby script/generate model ProfileNotification (singular) it worked. Naming conventions kill me. Thanks for all the help.
嗯,我想出了问题。当我运行 ruby 脚本/生成模型时,我正在输入 ruby 脚本/生成模型 ProfileNotifications。当我输入 ruby 脚本/生成模型 ProfileNotification(单数)时,它起作用了。命名约定杀了我。感谢所有的帮助。
回答by Adam Lassek
It's breaking because you're referencing Profile::ProfileNotificationwhich doesn't exist.
它正在破坏,因为您正在引用Profile::ProfileNotification不存在的内容。
Rails considers this a model named ProfileNotificationlocated in the Profilenamespace, but your comment suggests that Profileis another model class and not a namespace.
Rails 认为这是一个ProfileNotification位于Profile命名空间中的模型,但您的评论表明这Profile是另一个模型类而不是命名空间。
Based on the migration you have posted, I think you're confused about the Rails naming convention for one-to-many relationships. Here's how I think it's supposed to look:
根据您发布的迁移,我认为您对一对多关系的 Rails 命名约定感到困惑。这是我认为它应该看起来的样子:
class CreateNotifications < ActiveRecord::Migration
def self.up
create_table :notifications do |t|
t.references :profile
t.text :body
t.boolean :checked, :default => false
t.boolean :update_reply, :default => false
t.boolean :opinion_reply, :default => false
t.boolean :message_reply, :default => false
t.boolean :pm, :default => false
t.boolean :accepted_friend, :default => false
t.boolean :accepted_supporter, :default => false
t.timestamps
end
end
def self.down
drop_table :notifications
end
end
class Profile < ActiveRecord::Base
has_many :notifications
end
class Notification < ActiveRecord::Base
belongs_to :profile
end
Now when you execute Profile.find(1).notificationsyou should get a list of the associated notifications to that profile.
现在,当您执行时,Profile.find(1).notifications您应该会获得与该配置文件相关联的通知列表。
More information: Active Record Associations
更多信息:活动记录关联

