2010-11-30 53 views
7

我刚刚设置了一个新的迁移和模型关系,并且在控制台测试表之间的关系时出现以下错误:NameError:未初始化的常量。Ruby on Rails NameError:未初始化的常量

有没有人知道什么是错的?

谢谢

编辑:

这里的错误

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 

代码从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 
+1

你将不得不再给我们多一点继续...模型源也许? – karmajunkie 2010-11-30 03:06:26

+0

我有一个名为profiles的表,我创建了一个名为ProfileNotifications的新表。表格之间建立了一对多的关系。在配置文件模型中,我有:has_many:profile_notifications,并且在ProfileNotification模型中有:belongs_to:profile。在控制台中,输入user = Profile.find(1),然后输入user.profile_notifications,然后获取上面发布的错误消息。 – Brian 2010-11-30 03:42:22

+1

是模型ProfileNotifications或ProfileNotification中的类吗?在上面的评论中,您将它列为两者。 – 2010-11-30 03:54:53

回答

3

它打破,因为你引用Profile::ProfileNotification它没有按”不存在。

Rails认为这是一个名为ProfileNotification的模型位于Profile命名空间,但您的评论暗示Profile是另一个模型类而不是命名空间。

基于您发布的迁移,我认为您对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 

现在当你执行Profile.find(1).notifications你应该得到相关的通知,该配置文件的列表。

的更多信息:Active Record Associations

27

好吧,我想通了这个问题。当我运行ruby脚本/生成模型时,我正在键入ruby脚本/生成模型ProfileNotifications。当我输入ruby脚本/生成模型ProfileNotification(单数)它的工作。命名约定杀了我。感谢所有的帮助。

相关问题