I’ve got two models:
Customer and Contact
Customers table has columns :id, :firstName, :lastName
Contacts table has columns :id, :cid, :hphone, :cphone
So if Customers table has data
1 Josh McDonnel
Then Contacts table has corresponding
5 1 947-245-2342 342-543-8585
What associations can I use here?
Will Contact have
belongs_to :customer, :foreign_key => "id", :class_name => "Customer"
What should the Customer class have?
Also, how will a simple find_byXXX look like if I want to get all customers (firstName, lastName and corresponding hphone and cphone)
You are close, but your
belongs_toshould be this.:foreign_keyshould be the name of the field storing a reference to the primary id:And in your
Customer.rbclass:Finally, your search might look like this:
Then you could use it in a loop in your view:
Incidentally, if you had used
customer_idinstead ofcidyour association could just be: