Forum: Ruby on Rails Searching for an entry in a one to one relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lance K. (Guest)
on 2009-01-02 01:18
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.
unknown (Guest)
on 2009-01-02 11:04
(Received via mailing list)
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
This topic is locked and can not be replied to.