2012-04-12 75 views
0

我 customer.rb型號麻煩

class Customer < ActiveRecord::Base 
    acts_as_authentic 
    has_many :credit_cards 
    has_many :telephones 
    has_many :virtual_pays 
end 

telephone.rb

class Telephone < ActiveRecord::Base 
    belongs_to :customers 
end 

當我使用的軌道控制檯廣告寫Customer.all,我會得到從數據庫表中的所有數據客戶,但是當我嘗試獲取所有數據包括客戶電話我得到錯誤 我寫:customers.telephones.all,然後我嘗試Customer.Telephone.all,但如何正確地做到這一點?

NameError: undefined local variable or method customers' for main:Object from (irb):6 from /home/pp/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in start' from /home/pp/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in start' from /home/pp/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.9/lib/rails/commands.rb:23:in ' from script/rails:6:in require' from script/rails:6:in '

另外我正確的寫模型?

+0

你想知道如何查詢客戶的電話號碼嗎? – 2012-04-12 20:11:56

+0

其實是的.... – byCoder 2012-04-12 21:12:54

回答

1

在您的電話型號中,它應該是belongs_to:customer(單數)。

+0

和如何寫在控制檯? – byCoder 2012-04-13 13:26:09

+0

@pavel你會寫在你的模型,而不是控制檯。在您的模型中,您會將belongs_to:customers更改爲belongs_to:customer。然後加載控制檯並嘗試您的客戶呼叫。 – 2012-04-13 13:31:41

+0

))我知道,我問我應該在控制檯上寫什麼? customer.telephones?如果是這樣,它不工作 – byCoder 2012-04-13 15:37:43