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

Bri*_*ian 7 ruby-on-rails

我只是设置了一个新的迁移和模型关系,并且在测试表之间的关系时在控制台中我得到以下错误:NameError:uninitialized constant.

有谁知道什么是错的?

谢谢

编辑:

这是错误

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Bri*_*ian 27

好吧,我弄明白了这个问题.当我运行ruby脚本/生成模型时,我正在键入ruby脚本/生成模型ProfileNotifications.当我键入ruby脚本/生成模型ProfileNotification(单数)时,它工作.命名约定会杀了我.谢谢你的帮助.

  • 模型是单数,控制器是复数,表名是复数.生成器可能有点拐杖,我建议熟悉Rails希望你遵循的约定,否则你将有一段时间让这个工作:) (11认同)

Ada*_*sek 3

它正在破坏,因为您正在引用Profile::ProfileNotification不存在的内容。

ProfileNotificationRails 认为这是一个位于命名空间中的模型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
Run Code Online (Sandbox Code Playgroud)

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

详细信息:活动记录协会