假設我有三個型號,設立這樣的:預加載使用包括對不相關的模型
class Student < ActiveRecord::Base
has_many :tests
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :student
end
class Test < ActiveRecord::Base
belongs_to :student
end
我要查詢其學生不具備汽車的所有測試。預裝。
我已經試過如下:
Test.includes(:cars) # does not work because the two are not associated
Test.joins('inner join cars ON tests.student_id = cars.student_id') # works, but it doesn't preload the Cars model in my result
我不想創建一個has_many :through
關係,因爲他們真的在所有都沒有關係,但我不反對如果這是最好的解決方案。
想法?
的Rails 4.1.5
的PostgreSQL 9.3.4
紅寶石2.1.2
哦,我喜歡這個。爲什麼聯接效率低下?我認爲使用'includes'的全部目的是爲了減少查詢次數,從而提高效率? –
聯接很慢。當你有非常大的表格時,它們會以指數級的速度慢下來並使用更多的內存。在很多情況下,2個(或3個或更多)快速非連接查詢可能會更有效,並且隨着表大小的增加,它們的執行時間會線性增長,而不是呈指數級增長。 –
實際上,這實際上是一個很好的教訓 - 最好權衡各種選擇,而不僅僅是教科書所說的。特別是因爲教科書可能告訴你你可以做什麼,而不是你應該做什麼。 –