# Concept for foreign key

**URL:** <https://www.ruby-forum.com/t/concept-for-foreign-key/111017>\
**Category:** Rails\
**Created:** [August 1, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/concept-for-foreign-key/111017 "2007-08-01T23:46:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 1, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/concept-for-foreign-key/111017/1 "2007-08-01T23:46:31Z")

</div>

Hi

I have a table named “appointments” and it has a field “patient\_id”.  
And i have “patients” table as well, with all the patient’s personal  
info

now i want to display all the appointments for today with patients  
details. E.g

Name Phone Email Appointment  
Time  
ABC 123 removed\_email\_address@domain.invalid 01/08/2007 12:30  
PM

I have appointments Controller, helper, model but nothing patient so  
far.

Please advice

thanks

Ajit

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 2, 2007, 1:14am UTC](https://www.ruby-forum.com/t/concept-for-foreign-key/111017/2 "2007-08-02T01:14:37Z")

</div>

A patient has many appointments. An appointment belongs to patient.  
Appointment.patient.first\_name.

It’s really about that easy.

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 2, 2007, 2:02am UTC](https://www.ruby-forum.com/t/concept-for-foreign-key/111017/3 "2007-08-02T02:02:40Z")

</div>

On Aug 1, 2007, at 7:13 PM, Robert W. wrote:

> > info  
> > I have appointments Controller, helper, model but nothing patient so  
> > far.
> > 
> > Please advice
> > 
> > thanks
> > 
> > Ajit

Something in your controller like:

@appointments = Appointment.scheduled\_for(Date.today)

and in your model

class Appointment  
def self.scheduled\_for(day)  
find(:all, :include =\> :patient,  
:conditions =\> { :appointment\_time =\>  
day.to\_time…day.to\_time.tomorrow },  
:order =\> ‘appointments.appointment\_time, patients.last\_name’)  
end  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 2, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/concept-for-foreign-key/111017/4 "2007-08-02T16:27:15Z")

</div>

thank you guys…
