我有兩個型號:rails belongs_to has_one。需要一些解釋
Customer
和Contact
Customers
表中的列:id, :firstName, :lastName
Contacts
表中的列:id, :cid, :hphone, :cphone
因此,如果客戶表中有數據
1 Josh McDonnel
然後聯繫表中有相應的
5 1 947-245-2342 342-543-8585
我可以使用什麼關聯嗎?
將聯繫有
belongs_to :customer, :foreign_key => "id", :class_name => "Customer"
應該採取什麼Customer類有哪些?
此外,如何將一個簡單的find_byXXX
樣子,如果我想獲得所有客戶(firstName
,lastName
和相應hphone
和cphone
)
感謝您的一個很好的解釋。我試圖做你提出的搜索,但在我的情況'cid'是varchar,但'id'是整數。以便搜索不起作用並拋出錯誤。我可以回去並將cid更改爲整數,但是可以說例如我不想改變它。將會有什麼工作呢? – Omnipresent 2010-01-22 22:05:02
當然,只是運行一個遷移。 (備份你的數據!)運行這個:'script/generate migration ChangeContactsForeignKey'並使用下面的代碼:http://pastie.org/790513 – 2010-01-22 22:14:24
當然,當你完成這些步驟時使用'rake db:migrate' – 2010-01-22 22:21:38