In colour: http://pastie.caboo.se/117092
class HdrCurrentProgram < ActiveRecord::Base
set_table_name 'hdr_programs_lt_vw'
set_primary_keys ['student_id','stdnt_car_nbr']
belongs_to :candidature,
:class_name => 'HdrCurrentCandidature',
:foreign_key => [:student_id, :stdnt_car_nbr]
has_many :supervisors,
:class_name => 'HdrCurrentSupervision',
:foreign_key => [:acad_prog,:student_id]
end
class HdrCurrentSupervision < ActiveRecord::Base
set_table_name 'hdr_supervision_lt_vw'
set_primary_keys [:student_id,:super_role,:stdnt_super_nbr]
belongs_to :person,
:foreign_key => [:student_id]
end
and the generated sql:
SELECT *
FROM hdr_supervision_lt_vw WHERE ((hdr_supervision_lt_vw.acad_prog =
'1069416') AND (hdr_supervision_lt_vw.student_id = 0))
i.e. in a normal world i'd habe
Program (id) and Supervisors(id, program_id)
in my world i've got:
Program(student_id, acad_prog) and Supervisors(student_id, acad_prog)
(and acad_prog isin't part of the primary key of either)
So i'm assuming that when your defining the foreign_key, it has to be
the primary key of the reference model.
I switched to using finder_sql and wrote:
has_many :supervisors,
:class_name => 'HdrCurrentSupervision',
:finder_sql => 'SELECT *
FROM hdr_supervision_lt_vw
WHERE student_id = #{student_id} and
acad_code = #{acad_code}'
which seems to work quite nicely!