will_paginate 删除“sum”在第一页之后

发布于 2024-12-07 14:06:07 字数 1315 浏览 0 评论 0原文

我正在使用 will_paginate,它就像一个魅力,可以将我的结果分成许多页。但在除第一个页面之外的任何页面上,我的“sum”方法都会返回 0。

我的视图中有两个表。第一个是整个集合的摘要信息,第二个是分页集合。我收藏的第一页展示了一切完美。 Page >= 2 将“总和”变为 0,但 .count 仍然有效。

我的控制器:

def donations_by_season
  @season_name = params[:season_name]
  @donations = Donation.by_season(@season_name).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 25
  @valid_seasons = Donation.select(:season).group(:season)
end

我的部分在顶部表格中具有以下值:

    <td> <%= number_to_currency @donations.sum(:amount) %></td>
    <td><%= @donations.count %></td>

在下部表格中:

<% @donations.each do |donation| %>
<tr class="<%= cycle("odd", "even", :name => "row_class") %> ">
  <td><%= number_to_currency(donation.amount) %></td>
  <td><%= donation.iho_full %></td>
  <td><%= donation.box_code %></td>
  <td><%= donation.list_name %></td>
 </tr>
<% end %>
</table>

<p><%= will_paginate @donations %></p>

在第一页上,number_to_currency @donations.sum(:amount)@donations.count 是准确的。在第一页之后,.sum 变为 0,但 .count 保持不变。如何让 .sum 也发挥作用?

I am using will_paginate which works like a charm for breaking up my results into many pages. But on any page but the first one, my 'sum' method returns 0.

My view has two tables in it. The first is summary information over the entire collection and the second is the paginated collection. Page 1 of my collection shows everything perfect. Page >= 2 turns the 'sum' to 0 yet .count still works.

My controller:

def donations_by_season
  @season_name = params[:season_name]
  @donations = Donation.by_season(@season_name).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 25
  @valid_seasons = Donation.select(:season).group(:season)
end

My Partial has these values in the top table:

    <td> <%= number_to_currency @donations.sum(:amount) %></td>
    <td><%= @donations.count %></td>

And these in the lower:

<% @donations.each do |donation| %>
<tr class="<%= cycle("odd", "even", :name => "row_class") %> ">
  <td><%= number_to_currency(donation.amount) %></td>
  <td><%= donation.iho_full %></td>
  <td><%= donation.box_code %></td>
  <td><%= donation.list_name %></td>
 </tr>
<% end %>
</table>

<p><%= will_paginate @donations %></p>

On page one, number_to_currency @donations.sum(:amount) and @donations.count are accurate. After page one, the .sum turns to 0 but the .count remains the same. How do I get the .sum to work as well?

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

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

发布评论

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

评论(1

行至春深 2024-12-14 14:06:07

我相信 will_paginate 会覆盖关系方法“count”。

如果您查看分页方法返回的关系,它不包含它所说的计数元素。

总和也一样。它必须覆盖它,因为它不反映真实集合返回的内容。

如果您重构代码,使用:

@all_donations = Donation.by_season(@season_name)
@donations = @all_donations.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 25

并使用 @all_donations 来获取计数和总和指标,它应该可以工作。

I believe will_paginate is overriding the relation method "count".

If you take a look at the relation returned by the paginate method, it doesn't contain the elements that it says its counting.

The same for sum. It must be overwritting it, because it does not reflect what the true collection is returning.

If you refactor your code, to use:

@all_donations = Donation.by_season(@season_name)
@donations = @all_donations.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 25

And use @all_donations, to get the count and sum metrics, it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文