# Searching for an entry in a one to one relationship

**URL:** <https://www.ruby-forum.com/t/searching-for-an-entry-in-a-one-to-one-relationship/156726>\
**Category:** Rails\
**Created:** [January 2, 2009, 12:18am UTC](https://www.ruby-forum.com/t/searching-for-an-entry-in-a-one-to-one-relationship/156726 "2009-01-02T00:18:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 2, 2009, 12:18am UTC](https://www.ruby-forum.com/t/searching-for-an-entry-in-a-one-to-one-relationship/156726/1 "2009-01-02T00:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 2, 2009, 10:04am UTC](https://www.ruby-forum.com/t/searching-for-an-entry-in-a-one-to-one-relationship/156726/2 "2009-01-02T10:04:09Z")

</div>

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
