0

鉴于:

class ThreadParticipations
  scope :unread, lambda{ |user| where(:user_id => user.id, :read => false) }
end

ThreadParticipations
  .unread(current_user)
  .includes(:thread => :project)
  .group('projects.id')
  .count('threads.id')

哪个输出:

 => { 1 => 15, 3 => 10 }

如何使用该结果更新我的列表。结果集 1 和 3 中给出的是 project_ids,我如何遍历该结果以更新如下列表:

# iterates over @projects
<div>
 <li>Project_id 1, unread count = 15</li>
 <li>Project_id 2, unread count = 0</li>
 <li>Project_id 3, unread count = 10</li>
 <li>Project_id 4, unread count = 0</li>
 <li>Project_id 5, unread count = 0</li>
</div>

谢谢

4

2 回答 2

1

不确定我是否理解,您只是想迭代哈希以输出 html?

<ul>
    <% { 1 => 15, 3 => 10 }.each_pair do |k,v| %>
        <li>Project_id <%= k %>, unread count = <%= v%></li>
    <% end %>
</ul>

编辑:像那样?

<ul>
    <% [[1,1], [2, 3], [3, 15]].each do |project_id, unread_count| %>
        <li>Project_id <%= project_id %>, unread count = <%= unread_count %></li>
    <% end %>
</ul>
于 2011-02-06T04:31:48.770 回答
1

首先:您的 HTML 无效。li元素进入ul's 或ol's,而不是div's。您正在寻找的代码是:

<ul>
  <% hash.each do |project_id, unread_count| %>
    <li>Project_id <%= project_id %>, unread count = <%= unread_count %></li>
  <% end %>
</ul>
于 2011-02-06T04:32:30.440 回答