# Basic Active record help

**URL:** <https://www.ruby-forum.com/t/basic-active-record-help/74836>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836 "2006-11-07T10:58:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/1 "2006-11-07T10:58:38Z")

</div>

Hi all,

I am using Active record against a legacy schema (and I am a rails  
newbie). I am having trouble in getting the “belongs\_to” and “has\_many”  
relationships to work.

I have the following 2 tables in the schema:

PATIENT:  
PATIENT\_ID (PK)  
…

ELIGIBILITY\_INFORMATION:  
ELIGIBILITY\_ID (PK)  
PATIENT\_ID (FK)  
…

I have defined the following 2 classes:

class Patient \< ActiveRecord::Base

self.primary\_key = “PATIENT\_ID”  
self.table\_name = “PATIENT”

has\_many :eligibilities, :foreign\_key =\> “PATIENT\_ID”

end

class Eligibility \< ActiveRecord::Base

self.primary\_key = “ELIGIBILITY\_ID”  
self.table\_name = “ELIGIBILITY\_INFORMATION”

belongs\_to :patient

end

Here is my console output:

> > p = Patient.find(‘1’)  
> > …
> 
> > p.eligibilities  
> > []

In the DB eligibility\_information table has several eligibility records  
with PATIENT\_ID = ‘1’. Why is the p.eligibilities collection empty  
then? How do i correct this?

It looks like I am missing something basic here. Please help …

Thanks,

Rushabh

---

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/2 "2006-11-07T10:58:40Z")

</div>

has\_many :eligibilities, :foreign\_key =\> “PATIENT\_ID”

shouldn’t that be:

has\_many :eligibilities, :foreign\_key =\> “ELIGIBILITY\_ID”

–  
Andrew S.

---

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/3 "2006-11-07T10:58:46Z")

</div>

Andrew S. wrote:

> has\_many :eligibilities, :foreign\_key =\> “PATIENT\_ID”
> 
> shouldn’t that be:
> 
> has\_many :eligibilities, :foreign\_key =\> “ELIGIBILITY\_ID”

I dont think that is correct. PATIENT is the parent table. And so  
PATIENT\_ID would be the foreign key in the ELIGIBILITY\_INFORMATION  
table.

---

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/4 "2006-11-07T10:59:00Z")

</div>

> has\_many :eligibility

Andrew, this did not work ☹

---

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/5 "2006-11-07T10:59:58Z")

</div>

On 11/3/06, Rushabh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > has\_many :eligibility
> 
> Andrew, this did not work ☹

Sorry to hear that, getting the same error?

–  
Andrew S.

---

<div class="post-metadata">

**Author:** ![rushabh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f9ae1b/32.png) [@rushabh](https://www.ruby-forum.com/u/rushabh)\
**Post date:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/basic-active-record-help/74836/6 "2006-11-07T10:58:56Z")

</div>

> I dont think that is correct. PATIENT is the parent table. And so  
> PATIENT\_ID would be the foreign key in the ELIGIBILITY\_INFORMATION  
> table.

Ooops. You’re right. Sorry about that…

has\_many :eligibility

–  
Andrew S.
