繁体   English   中英

Ruby on Rails NameError:未初始化的常量

[英]Ruby on Rails NameError: uninitialized constant

我只是建立了一个新的迁移和模型关系,并且在控制台中测试表之间的关系时收到以下错误: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

好吧,我发现了问题所在。 当我运行ruby脚本/生成模型时,我在键入ruby脚本/生成模型ProfileNotifications。 当我输入ruby脚本/生成模型ProfileNotification(单数)时,它起作用了。 命名约定使我丧命。 感谢您的所有帮助。

中断是因为您引用的Profile::ProfileNotification不存在。

Rails认为这是一个位于Profile命名空间中的名为ProfileNotification的模型,但是您的注释表明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您应该获得与该概要文件相关联的通知的列表。

详细信息: 活动记录关联

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM