rushabh
November 7, 2006, 10:58am
1
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
rushabh
November 7, 2006, 10:58am
2
has_many :eligibilities, :foreign_key => “PATIENT_ID”
shouldn’t that be:
has_many :eligibilities, :foreign_key => “ELIGIBILITY_ID”
–
Andrew S.
rushabh
November 7, 2006, 10:58am
3
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.
rushabh
November 7, 2006, 10:59am
4
has_many :eligibility
Andrew, this did not work
rushabh
November 7, 2006, 10:59am
5
On 11/3/06, Rushabh [email protected] wrote:
has_many :eligibility
Andrew, this did not work
Sorry to hear that, getting the same error?
–
Andrew S.
rushabh
November 7, 2006, 10:58am
6
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.