Ruby on rails Rails 4,如何使用自定义方法进行订购

Ruby on rails Rails 4,如何使用自定义方法进行订购,ruby-on-rails,ruby,ruby-on-rails-4,kaminari,Ruby On Rails,Ruby,Ruby On Rails 4,Kaminari,我想使用自定义方法订购对话模型 我找到了一些解决办法: 及 , 但谈话顺序优先 第一个-回答百分比描述, 第二个顺序是最后的回答时间 使用自定义模型方法最后回答了 最后一次回答方法来源: def last_answered_to_i if Conversation.where(company_id: self.id, is_answered: true).present? last_conversation = Conversation.where(company_id: self.

我想使用自定义方法订购对话模型

我找到了一些解决办法:

,

但谈话顺序优先

第一个-回答百分比描述, 第二个顺序是最后的回答时间 使用自定义模型方法最后回答了

最后一次回答方法来源:

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
订购后,我想使用kaminari gem添加分页


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

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

在您的情况下,您可以按如下自定义方法对对话进行排序: Conversation.all.sort\u by&:custom\u方法

或者具体地说: 对话.全部.排序依据&:最后一次回答

具体地说,您不能使用SQL按实际数据库中不存在的内容进行排序或排序,因此使用Ruby sort_by方法。有关符号和的更多信息,请参阅

对于你的实际观点,我不确定你到底想如何组织它。我最近做了一件事,需要用另一个名为categories的资源对我的资源进行分组,然后用netvots(一种自定义模型方法)对原始资源进行排序,然后按名称排序。我是这样做的:

在控制器中按名称排序:@resources=Resource。顺序:name 在视图的外部循环中按类别分组: 然后在部分资源中按投票对资源进行排序: 视图有点混乱,下面是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 %>
下面是_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>

我希望这能帮助你们思考更多的方法来解决你们的问题。

你们的人际关系在和公司的对话中是什么样子的?看来你们可以根据范围来处理这个问题。另外,我定义的最后一次对话在哪里?@engineersmnky Company有许多对话和方法定义的对话模型,所以您想返回某个公司或所有公司的所有对话?按[-conversation.answer\u percentage,conversation.last\u response\u to\u i]排序?只是试着理解所需的输出,以便我能设计出一个合适的答案。@engineersmnky是的,我想按[-conversation.answer\u percentage,conversation.last\u responsed\u to\u I]排序。结果ActiveRecord是所有人的对话company@engineersmnky使用公司的所有对话答案百分比按所有公司排序只需回答_count/问题_count*100和公司上一次对话的答案术语使用上一次回答_to _i
<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>