2010-11-30 55 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(單數)它的工作。命名約定殺了我。感謝所有的幫助。

相關問題