我已經創建了兩個模型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'沒找到;也許你拼錯了?「任何一個能幫助我嗎?
你所說的「訪問聯繫人表的主鍵爲客戶表」的意思是列的名字?您客戶數據庫表中是否已有'contact_id`列? – Gareth 2011-02-17 12:06:08
嗨Gareth,感謝您的回覆,我的客戶數據庫表中有一個'contact_id'列。 – 2011-02-18 04:02:53