2011-02-17 138 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 

而且你也不需要指定谁包含外键

(对不起,我的英语)