Basic Active record help

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

has_many :eligibilities, :foreign_key => “PATIENT_ID”

shouldn’t that be:

has_many :eligibilities, :foreign_key => “ELIGIBILITY_ID”

Andrew Stone 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.

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

has_many :eligibility

Andrew, this did not work :frowning:

Sorry to hear that, getting the same error?