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}

但迭代多次

4

1 回答 1

0

您必须定义自定义关联而不是范围并将其用于预加载:

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}

看到这个问题

于 2013-06-28T09:16:13.003 回答