简体   繁体   English

Rails 4,如何使用自定义方法订购

[英]Rails 4, how to ordering using custom method

I want to order the Conversation model, using a custom method. 我想使用自定义方法订购Conversation模型。

I found some solution: 我找到了一些解决方案:

How do you order by a custom model method that has no attribute in SQL? 如何通过SQL中没有属性的自定义模型方法进行排序?

and

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

but Conversation order have priority. Conversation顺序优先。

First- answer_percent desc , second- order to last_answer time (using custom model method last_answered_to_i ). 一阶answer_percent desc ,二阶至last_answer时间(使用自定义模型方法last_answered_to_i )。

last_answered_to_i method source: last_answered_to_i方法源:

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. 订购后,我想使用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. 我认为答案取决于您想在视图中看到的内容,因为某些问题实际上可以通过调用那里的@lists来解决。 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) 在您的情况下,您可以按照以下自定义方法对会话进行排序: Conversation.all.sort_by(&:custom_method)

Or specifically: Conversation.all.sort_by(&:last_answered_to_i) 或特别是: 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. 具体来说,您不能使用SQL对实际数据库中未存在的内容进行排序或排序,因此您可以使用Ruby sort_by方法。 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. 最近,我做了一些需要将资源按另一个称为“类别”的资源进行分组的工作,然后按作为自定义模型方法的“ netvotes”对原始资源进行排序,然后按名称进行排序。 I did it by: 我这样做是:

  • Ordering by name in the controller: @resources = Resource.order(:name) 在控制器中按名称排序: @resources = Resource.order(:name)
  • Grouping by category in the outer loop of the view: <% @resources.group_by(&:category).each do |category, resources| %> 在视图的外部循环中按类别分组: <% @resources.group_by(&:category).each do |category, resources| %> <% @resources.group_by(&:category).each do |category, resources| %>
  • Then sorting the resources by votes in the partial for resources: <%= render resources.sort_by(&:netvotes).reverse %> 然后按部分资源对资源进行投票排序: <%= render resources.sort_by(&:netvotes).reverse %>

The view is a bit confusing, so here is the full view loop in index.html.erb: 视图有点混乱,因此这是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: 这是_resource.html.erb部分:

<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. 我希望这可以帮助您通过其他一些方法来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM