好的,我正在做这个
<% @courses.each do |course| %>
<tr>
<th>Users</th>
</tr>
<tr>
<td>
<span><%= course.not_competent %></span> <span><%= course.novice %></span></td>
</tr>
<% end %>
在我的类(class)模型中我有这个
def novice
self.courses_users.low_scores.collect{|score| score.user.username}.join(', ')
end
def not_competent
self.courses_users.really_low_scores.collect{|score| score.user.username}.join(', ')
end
问题是这种方法会运行数以千计的查询....有没有更好的方法不会降低数据库
最佳答案
对@Zabba,首先尝试更改填充@courses 的调用并添加类似于此参数的内容
:include => [{:courses_users => :low_scores}]
或链
.includes([{:courses_users => :low_scores}])
(我想 low_scores 是你模型的另一个关联)
以这种方式,Activerecord 更改了包含嵌套表的第一个查询。在此 ascii_cast一些例子。
希望对你有用
关于ruby-on-rails - 我如何消除一些查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938908/