0
has_many : comments
belongs_to : user
scope : more_no comments where(self.no_times >10)
查詢:這樣我試圖
User.includes(:comments).all.collect{|u| u.comments.more_no_comments}
但迭代多次
has_many : comments
belongs_to : user
scope : more_no comments where(self.no_times >10)
查詢:這樣我試圖
User.includes(:comments).all.collect{|u| u.comments.more_no_comments}
但迭代多次
你必須定義一個自定義關聯而不是範圍,並將其用於熱切加載:
has_many :more_no_comments, :class_name => 'Comment', -> { where "no_times > 10" }
然後
User.includes(:more_no_comments).all.collect{|u| u.comments.more_no_comments}
爲什麼這不能沒有自定義關聯? –
'includes'適用於模型的關聯,範圍在* associated *模型上定義 –