你可以這樣做:
Appointment.find(:all, :include => {:patient}, :order => 'people.last_name')
你在做什麼是抓住所有的約會,他們在同一時間相關的患者。您不必擔心患者與醫生之間的關係,因爲所有檢索到的人員都是患者記錄。
爲了建立以醫生爲中心的清單,只需將患者更改爲:上例中的醫生。
編輯:我想出瞭解決方案,當你渴望負載病人和醫生。它變得有點複雜。首先,我重新創建你的4款車型中的空白Rails應用程序的簡單版本,然後試圖運行找到:order => 'patients.name'
:
Appointment.find(:all, :include => [:patient, :doctor], :order => 'patients.name')
當然,如果失敗了,但它也吐出SQL查詢它試圖:
SELECT
"appointments"."id" AS t0_r0,
"appointments"."name" AS t0_r1,
"appointments"."doctor_id" AS t0_r2,
"appointments"."patient_id" AS t0_r3,
"appointments"."created_at" AS t0_r4,
"appointments"."updated_at" AS t0_r5,
"people"."id" AS t1_r0,
"people"."name" AS t1_r1,
"people"."type" AS t1_r2,
"people"."created_at" AS t1_r3,
"people"."updated_at" AS t1_r4,
"doctors_appointments"."id" AS t2_r0,
"doctors_appointments"."name" AS t2_r1,
"doctors_appointments"."type" AS t2_r2,
"doctors_appointments"."created_at" AS t2_r3,
"doctors_appointments"."updated_at" AS t2_r4
FROM "appointments"
LEFT OUTER JOIN "people" ON "people".id = "appointments".patient_id AND ("people"."type" = 'Patient')
LEFT OUTER JOIN "people" doctors_appointments ON "doctors_appointments".id = "appointments".doctor_id AND ("doctors_appointments"."type" = 'Doctor')
ORDER BY patients.name
現在我們可以看到rails如何形成這樣的查詢。使用給定表的第一個關聯直接獲取表名 - 「人員」。隨後的關聯會獲得關聯和原始表的組合 - 「doctors_appointments」。
它看起來可能有點亂,但這個電話給你的病人下令:
Appointment.find(:all, :include => [:patient, :doctor], :order => 'people.name')
而這一次給你的醫生下令:
Appointment.find(:all, :include => [:patient, :doctor], :order => 'doctors_appointments.name')
當然,在我的例子中,我只是每個人都有一個簡單的姓名字段,您將使用「last_name」代替。但你明白了。這對你有用嗎?
最後編輯:
我會把這些在發現者,所以你並不需要在代碼中其他地方的表名混亂。我會這樣做:
class << self
def order_by_patient(field='last_name')
find(:all, :include => [:patient, :doctor], :order => "people.#{field}")
end
def order_by_doctor(field='last_name')
find(:all, :include => [:patient, :doctor], :order => "doctors_appointments.#{field}")
end
end
現在你可以從任何地方打電話給他們,甚至根據你想要的字段排序。
您在使用STI?另外,你可以發佈整個失敗的AR查找或named_scope嗎?如果有病人表,那麼你可能應該這樣做:order =>'patients.last_name'(複數),但我們不能確定你的基礎表是什麼樣的... – hgmnz 2010-01-19 19:57:46
我更新了我的問題 – Tony 2010-01-19 20:49:42