You didn't say if it exists, but I'm guessing that there's an 'encounter group' table associated to that number (99999). In which case, what you're looking at is a has_many :through relation.
(code example simplified - fix up with modules, etc as your app needs)
class Patient < ActiveRecord::Base # has field pat_encounter_group belongs_to :encounter_group, :foreign_key => 'pat_encounter_group' has_many :requests, :through => :encounter_group end
class EncounterGroup < ActiveRecord::Base has_many :patients, :foreign_key => 'pat_encounter_group' has_many :requests, :foreign_key => 'req_encounter_group' end
class Request < ActiveRecord::Base belongs_to :encounter_group, :foreign_key => 'req_encounter_group' has_many :patients, :through => :encounter_group end
[Note: I haven't tried this code, but it should work according to the documentation]
On the other hand, the code you've got below should, in principle, also work. How exactly did it "fail"?
--Matt Jones