Searching for an entry in a one to one relationship


#1

1] I had a “Patient” model which is in a one to one relationship with
the “PersonName” model

class Patient < ActiveRecord::Base
has_one :person_name
end

class PersonName < ActiveRecord::Base
belongs_to :patient
end

2] “person_name” has the “last_name” and “first_name” as 2 of the fields

3] I am trying to find the Patient on the basis of his last_name , how
do i go about doing this , any example’s will be HIGHLY appreciated.

The way i entered the data in was first create a PersonName

@personName = PersonName.new ( :last_name => “jack” , :first_name =>
“daniels”)
@patient.person_name = @personName

Thanks in advance.


#2

Use the Patient and PersonName relationship I.e. :
@person = PersonName.find_by_last_name
Then a simple @person.patient will return the patient class object since
rails knows the relationship. I would just include the first_name and
last_name fields in the patient table. Having a seperate datafield to
hold that little of info seems silly. You could use methods within the
patient class to seperate the first_name and last_name data instead.
Sent from my Verizon Wireless BlackBerry