我已经创建了两个模型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