Rails 4,如何使用自定义方法进行排序 [英] Rails 4, how to ordering using custom method

查看:59
本文介绍了Rails 4,如何使用自定义方法进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自定义方法订购 Conversation 模型.

我找到了一些解决方案:

如何通过在 SQL 中没有属性的自定义模型方法进行排序?

http://awaxman11.github.io/blog/2013/10/11/sorting-a-rails-resource-based-on-a-calculated-value/,

Conversation顺序有优先权.

第一 - answer_percent desc,last_answer 时间的二阶(使用自定义模型方法 last_answered_to_i ).

last_answered_to_i 方法来源:

def last_answered_to_i如果 Conversation.where(company_id: self.id, is_answered: true).present?last_conversation = Conversation.where(company_id: self.id, is_answered: true).first如果 last_conversation.answered_at.blank?||last_conversation.asked_at.blank?分钟 = (Time.now- last_conversation.updated_at)/1.minutes别的分钟 = (last_conversation.answered_at - last_conversation.asked_at)/1.minutes结尾分钟.to_i别的零结尾结尾

订购后我想使用 kaminari gem 添加分页.

@lists = Company.searchable.order("answer_percent desc").page(params[:page]).per(20)

如何按列和自定义方法排序并添加分页?

解决方案

我认为答案取决于您希望在视图中看到的内容,因为有些问题实际上可以通过您在那里调用 @lists 的方式来解决.此外,您发现的一些链接使按模型方法排序听起来比实际更困难.

就您而言,您可以通过自定义方法对对话进行排序,如下所示:Conversation.all.sort_by(&:custom_method)

或者具体来说:Conversation.all.sort_by(&:last_answered_to_i)

特别是,您不能使用 SQL 对不在实际数据库中的内容进行排序或排序,因此您使用 Ruby sort_by 方法.有关 & 符号的更多信息,请参阅这篇文章.

对于您的实际视图,我不确定您想如何组织它.我最近做了一些事情,我需要按另一个名为类别"的资源对我的资源进行分组,然后按netvotes"(一种自定义模型方法)对原始资源进行排序,然后按名称排序.我是这样做的:

  • 在控制器中按名称排序:@resources = Resource.order(:name)
  • 在视图的外循环中按类别分组:<% @resources.group_by(&:category).each do |category, resources|%>
  • 然后按资源部分中的投票对资源进行排序:<%= render resources.sort_by(&:netvotes).reverse %>

视图有点混乱,所以这里是 index.html.erb 中的完整视图循环:

<% @resources.group_by(&:category).each do |category, resources|%><div class="well"><h3 class="brand-text"><%= category.name %></h3><%=render resources.sort_by(&:netvotes).reverse %>

<%结束%>

这里是 _resource.html.erb 部分:

<div class="col-sm-2 text-center"><div class="vote-box"><%= link_to fa_icon('chevron-up lg'), upvote_resource_path(resource), method: :put %><br><%=resource.netvotes%><br><%= link_to fa_icon('chevron-down lg'), downvote_resource_path(resource), method::put %>

<div class="col-sm-10"><%= link_to resource.name, resource.link, target: "_blank" %><p><%=resource.notes%></p>

我希望这能帮助您思考解决问题的更多方法.

I want to order the Conversation model, using a custom method.

I found some solution:

How do you order by a custom model method that has no attribute in SQL?

and

http://awaxman11.github.io/blog/2013/10/11/sorting-a-rails-resource-based-on-a-calculated-value/ ,

but Conversation order have priority.

First- answer_percent desc, second- order to last_answer time (using custom model method last_answered_to_i ).

last_answered_to_i method source:

def last_answered_to_i
  if Conversation.where(company_id: self.id, is_answered: true).present?
    last_conversation = Conversation.where(company_id: self.id, is_answered: true).first
    if last_conversation.answered_at.blank? || last_conversation.asked_at.blank?
      minutes =  (Time.now- last_conversation.updated_at)/1.minutes
    else
      minutes =  (last_conversation.answered_at - last_conversation.asked_at)/1.minutes
    end
    minutes.to_i
  else
    nil
  end
end

after ordering I want add pagination using kaminari gem.

@lists = Company.searchable.order("answer_percent desc").page(params[:page]).per(20)

How do I order by column and custom method and add pagination?

解决方案

I think the answer depends on what you want to see in the view because some of the problem could actually be solved in how you call @lists there. Also, some of the links you found make sorting by a model method sound more difficult than it is.

In your case, you can sort your conversations by a custom method like so: Conversation.all.sort_by(&:custom_method)

Or specifically: Conversation.all.sort_by(&:last_answered_to_i)

Specifically, you cannot use SQL to sort or order by something not in the actual database, so you use the Ruby sort_by method. For more info on the ampersand, see this post.

For your actual view, I'm not sure really how you want to organize it. I recently did something where I needed to group my resource by another resource called "categories", and then sort the original resource by "netvotes" which was a custom model method, then order by name. I did it by:

The view is a bit confusing, so here is the full view loop in index.html.erb:

<% @resources.group_by(&:category).each do |category, resources| %>
  <div class="well">
    <h3 class="brand-text"><%= category.name %></h3>
    <%= render resources.sort_by(&:netvotes).reverse %>
  </div>
<% end %>

And here is the _resource.html.erb partial:

<div class="row resource">
  <div class="col-sm-2 text-center">
    <div class="vote-box">
      <%= link_to fa_icon('chevron-up lg'), upvote_resource_path(resource), method: :put %><br>
      <%= resource.netvotes %><br>
      <%= link_to fa_icon('chevron-down lg'), downvote_resource_path(resource), method: :put %>
    </div>
  </div>
  <div class="col-sm-10">
    <%= link_to resource.name, resource.link, target: "_blank" %>
    <p><%= resource.notes %></p>
  </div>
</div>

I hope that helps you think through some more ways to address your problem.

这篇关于Rails 4,如何使用自定义方法进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆