使用基于聚合的动态过滤器的 Filter Rails 3 模块

发布于 2024-10-09 11:54:31 字数 491 浏览 0 评论 0原文

我有一个模型,其中包含公司名称、发布(日期)、位置(纬度/经度对)等属性。在索引视图中,我使用 will_paginate gem 对结果集进行分段。

我想包括基于查询结果的动态过滤器。例如,如果选择老化桶,则其他过滤器将基于此限制。理想情况下,我能够按频率对某些值进行排序。比如:

Posted: [ 0 (24) | 30 (51) | 60 (45) | 90+ (555) ]
Company: [ Company X (53) | Company AAA (44) | ... ] 
Locations: [ New York (100) | Paris (51) | ... ]

我考虑过使用辅助函数来根据结果的集合来计算聚合,但是 will_paginate gem 的集合(例如@foos)仅包含当前页面的结果。也许有一种方法可以访问整个结果集。

我想我可以执行一系列 SQL 语句来计算每个过滤器的频率值,但我希望可能有一种更有效的方法来执行此操作。

I have a model that includes attributes like company_name, posted (a date), location (lat/lng pair). In the Index view, I segment the result set using the will_paginate gem.

I would like to include dynamic filters that are based on the query's results. For example, if an aging bucket is chosen, the other filters would be based on this restriction. Ideally, I would be able to sort some of the value by frequency. Something like:

Posted: [ 0 (24) | 30 (51) | 60 (45) | 90+ (555) ]
Company: [ Company X (53) | Company AAA (44) | ... ] 
Locations: [ New York (100) | Paris (51) | ... ]

I thought about using a helper function to calculate the aggregates based on the result's collection, but the will_paginate gem's collection (e.g. @foos) only includes the current page's result. Perhaps there is a way to access the entire result set.

I suppose I could execute a series of SQL statements to calculate each filter's frequency values, but I was hoping there might be a more-efficient way of doing this.

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

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

发布评论

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

评论(1

绿光 2024-10-16 11:54:31

我通过以下方法取得了一些成功:

controller:

...
# WHERE clause
@foos = @foos.posted(params[:posted]) if params[:posted].present?

# clone recordset
@companies = @foos.clone

# set ORDER BY / pagination
@foos = @foos.order_by(params[:sort], params[:dir]).paginate(:per_page => Foo.per_page, :page => params[:page])
...

view:

...
Company: [
<% @companies.group_by(&:company_name).sort.each do |company, foos| %>
    <%= "#company.name} (#{foos.length})" %> | 
<% end %>
]
...

也许有一种更干燥/更有效的方法来做到这一点。

I've had some success with the following approach:

controller:

...
# WHERE clause
@foos = @foos.posted(params[:posted]) if params[:posted].present?

# clone recordset
@companies = @foos.clone

# set ORDER BY / pagination
@foos = @foos.order_by(params[:sort], params[:dir]).paginate(:per_page => Foo.per_page, :page => params[:page])
...

view:

...
Company: [
<% @companies.group_by(&:company_name).sort.each do |company, foos| %>
    <%= "#company.name} (#{foos.length})" %> | 
<% end %>
]
...

Perhaps there is a DRYer/more-efficient way to do this.

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