Ruby on Rails名称错误:未初始化的常量

8

我刚刚设置了一个新的迁移和模型关系,在控制台测试表之间的关系时,出现了以下错误: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

来自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
你需要提供更多的信息...比如模型来源? - Keith Gaddis
我有一个名为profiles的表,我创建了一个名为ProfileNotifications的新表。这两个表之间建立了一对多的关系。在profile模型中,我有:has_many:profile_notifications,在ProfileNotification模型中,我有:belongs_to:profile。在控制台中,我键入user = Profile.find(1),然后键入user.profile_notifications,但是我收到了上面发布的错误消息。 - Brian
1
在模型中,类是ProfileNotifications还是ProfileNotification?在您上面的评论中,两者都有列出。 - Ryan Bigg
迁移被命名为CreateProfileNotifications。 - Brian
2个回答

27

好的,我找到了问题所在。当我运行ruby脚本生成模型时,我输入了ruby script/generate model ProfileNotifications。当我输入ruby script/generate model ProfileNotification(单数形式),它就可以工作了。命名约定真让我头痛。谢谢你们的所有帮助。


11
模型使用单数,控制器使用复数,表名也使用复数。生成器可以成为一种依靠,但我建议熟悉Rails期望你遵循的惯例,否则你将会花费很多时间来让它工作 :) - Adam Lassek
顺便说一下,我尝试通过编辑模型来修复它,但仍然没有起作用。我猜你必须回滚迁移,删除所有创建的文件,并重新生成迁移。好消息是它告诉你所有创建的文件。 - B Seven

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

我有一个用于网站用户的个人资料表。我想创建另一个名为“通知”的表,以存储诸如用户何时收到新的私人消息回复等信息。然后,我将使用此表来在有人回复他们的私人消息时提醒会员。我发布了迁移代码,但似乎引起了错误。我感到困惑,因为我已经用这种方式设置了其他一对多表,并且从未遇到过这个问题。 - Brian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接