2011-02-17 107 views
0

我已經創建了兩個模型1)聯繫2)客戶在我的Rails應用程序軌工作,現在我想加入這些模型的兩個表。表格分別是聯繫人&客戶。我使用以下代碼:的has_many和belongs_to的不

1)contact.rb:

class Contact < ActiveRecord::Base 
    unloadable 
    has_many :customers 
end 

2)customer.rb

class Customer < ActiveRecord::Base 
    belongs_to :contact, :foreign_key => :contact_id` 
end 

3)customers_controller.rb

def new 
    @customer = Customer.new 
    @customer = Customer.find(:all,:include => :contact_id) 
end 

我在這裏試圖訪問聯繫表的主鍵到客戶表中,但它反覆給出這個錯誤「關聯名爲'contact_id'沒找到;也許你拼錯了?「任何一個能幫助我嗎?

+0

你所說的「訪問聯繫人表的主鍵爲客戶表」的意思是列的名字?您客戶數據庫表中是否已有'contact_id`列? – Gareth 2011-02-17 12:06:08

+0

嗨Gareth,感謝您的回覆,我的客戶數據庫表中有一個'contact_id'列。 – 2011-02-18 04:02:53

回答

0

當您使用include,你應該在協會名稱傳遞(你的情況」 contact「),而不是外鍵。

然而,您的描述不明確,這是你想做的事,所以如果你能澄清你的問題,如果它是錯的

0

如果我清楚地瞭解你不需要在變複數的客戶我會更新這個答案接觸模型:

class Contact < ActiveRecord::Base 
    unloadable 
    has_many :customers 
end 

而且你也不需要指定誰包含外鍵

(對不起,我的英語)