2013-02-19 58 views
0

我一直無法找到解決此問題的方法。這與之前提到的許多問題類似,但我相信我的情況有所不同。我目前正在此錯誤:NameError在帳戶#顯示,Ruby on Rails

NameError in Account#show 
uninitialized constant Account::AccountProces 

它引發錯誤在我看來:

<%@account.account_process.each do |process|%> 
    Name: <%=process.name%><br/> 
<%end%> 

現在我已籤的名字,一切都相匹配,除了AccountProces應該AccountProcess。我不知道爲什麼它顯示AccountProces只有一個。我搜索了我的整個目錄中只有1秒的內容。我還沒有找到任何東西。 這裏是我的模型:

class AccountProcess < ActiveRecord::Base 
    attr_accessible :account_id, :name 
    validates :account_id, presence: true 
    validates :name, presence: true 

    belongs_to :account 
    def as_json options={} 
    { 
     id: id, 
     name: name, 
     open_count: open_count, 
     created_at: created_at, 
     update_at: updated_at 

    } 
    end 
end 

這裏是我的移民:

class CreateAccountProcesses < ActiveRecord::Migration 
    def change 
     create_table :account_processes do |t| 
     t.references :account 

     t.string :name, :null => false, :default => "" 

     t.timestamps 
     end 
    end 
end 

這裏是我的簡化帳戶型號:

class Account < ActiveRecord::Base 
    attr_accessible :computer_id, :allotted_time, :domain, :tracking, :used_time, :username, :account_process_attributes 

    validates :username, :presence => true 
    validates :computer_id, :presence => true 

    has_many :account_process, :dependent => :destroy 

    accepts_nested_attributes_for :account_process 

    def as_json options={} 
    { 
    id: id, 
    computer_id: computer_id, 
    domain: domain, 
    username: username, 
    tracking: tracking, 

    account_process_attributes: account_process, 

    created_at: created_at, 
    update_at: updated_at 
    } 
    end 
end 

這應該是我能想到那會的一切導致這個問題。我還有其他屬性,如歷史和程序等。它們與賬戶流程幾乎完全相同,並且不會引發任何錯誤。它會在我的視圖中以及當我嘗試使用REST API保存到數據庫時引發此錯誤。

回答

0

嘗試改變

class Account < ActiveRecord::Base 
... 
    has_many :account_process, :dependent => :destroy 
... 
end 

class Account < ActiveRecord::Base 
... 
    has_many :account_processes, :dependent => :destroy 
... 
end 

,改變這種關聯在您看來太。

has_many協會名稱應始終使用複數的ActiveRecord將單個化的關聯名稱確定類。

如果無法正確生成類的名稱,你可以做這樣的事情:

has_many :account_processes, :class_name => "AccountProcess" 
+0

這給了我這個錯誤: 沒有相關發現名字'account_process'。它已被定義? – Rizowski 2013-02-19 23:13:58

+0

您應該重命名爲這種關聯您的來電太(像這裏'@ account.account_process.each') – Deradon 2013-02-19 23:15:48

+0

好吧,我將不得不讓它使用複數工作,但它的工作一點我能得到它的工作通過將:class_name => "AccountProcess"添加到我的協會。它給了我一個undefined method 'key?' for nil:NilClass錯誤。 – Rizowski 2013-02-19 23:32:29