给定一个返回 [1, 1] [2, 3], [5,1] 的查询,如何使用该结果?

发布于 2024-10-15 20:09:02 字数 777 浏览 1 评论 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>

谢谢

Given:

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')

Which outputs:

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

How do I use that result to update my list. Given in the result set 1 & 3 are project_ids, how can I iterate through that results to update a list like:

# 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>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-10-22 20:09:02

不确定我是否理解,您只是想迭代哈希来输出 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>

Not sure I understand, you just want to iterate on the hash to output the html?

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

edit: like that?

<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>
梦途 2024-10-22 20:09:02

首先:您的 HTML 无效。 li 元素位于 ulol 内部,而不是 div 内部。您要查找的代码是:

<ul>
  <% hash.each do |project_id, unread_count| %>
    <li>Project_id <%= project_id %>, unread count = <%= unread_count %></li>
  <% end %>
</ul>

First off: Your HTML is invalid. li elements go inside ul's or ol's, not div's. The code you're looking for is:

<ul>
  <% hash.each do |project_id, unread_count| %>
    <li>Project_id <%= project_id %>, unread count = <%= unread_count %></li>
  <% end %>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文