[英]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.