我似乎无法得到这个循环正确红宝石环路问题
@mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
unless @mr.empty?
@mr.each do |rating|
@m = Media.where("id = ?", rating.media_id)
@m.each do |m|
@history << m
end
end
end
工作在MediaRating表只有3个数据的测试行,但是当我在屏幕上输出的数据@history
数组包含9个条目。我知道它与我在循环中做错了什么有关,但无法弄清楚。
工作代码
查看:
<!-- For Each -->
<% unless @history.empty? %>
<% @history.each do |m| %>
<tr class="control_result_row">
<td class="control_result"><%= m.first.title %></td>
<td class="control_result">Russian</td>
</tr>
<% end %>
<% else %>
<tr class="control_result_row">
<td class="control_result" colspan="2">You have no ratings logged</td>
</tr>
<% end %>
<!-- End For Each -->
控制器:
@mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
@mr.each do |rating|
@m = Media.where("id = ?", rating.media_id)
@history << @m
end
媒体包含什么?在嵌套Media.where调用 – 2010-12-22 02:04:19
之后检查@m包含的内容:使用嵌入式“除非/ else”逻辑的ERB。当我执行代码评论或处理其他人的代码时,我更喜欢在代码中看不到该代码组合。我发现组合使我的大脑降档,以弄清楚发生了什么,因为我解释了“除非”中暗示的“不”。见约翰Nunemaker的[“除非,滥用Ruby有条件”](http://railstips.org/blog/archives/2008/12/01/unless-the-abused-ruby-conditional/)文章。 – 2010-12-22 05:41:31