html - 无法弄清楚如何将结果限制为希望使用 .group

标签 html ruby-on-rails ruby

我正在尝试创建一个下拉列表来过滤表格中的结果。到目前为止,除了列出多个项目外,一切正常。

如:

Clec ID:
216 324
216 315

因为我只想要一个 216。

    <select name="by_clecid">
      <option value="">All</option>
      <% @task_queues.each do |task_queue| %> <option value="<%= task_queue.clecid %>"><%= task_queue.clecid %></option><% end %> 
    </select>

我想我可以使用 .group 但不确定如何使用它。任何帮助将不胜感激我对 Ruby 不是很熟悉。

编辑: 型号:

class TaskQueue < ActiveRecord::Base

  require 'task_queue'

  self.table_name = "taskqueue"
  belongs_to :clec_profile
  scope :by_status,     -> status  { where(:status => status) }
  scope :by_tasktype,   -> tasktype  { where(:tasktype => tasktype) }
  scope :by_clecid,     -> clecid  { where(:clecid => clecid) }
  scope :by_taskid,     -> taskid  { where(:taskid => taskid) }
  scope :by_hostname,     -> hostname  { where(:hostname => hostname) }

  def elapsed_seconds
    case status
    when "Completed"
      finishtime - starttime
    when "Pending"
      Time.now - starttime
    when "Waiting", "Failed"
      0
    end
  end

end

一些 Controller :

class TaskQueuesController < ApplicationController
  before_action :set_task_queue, only: [:show, :edit, :update, :destroy]
  has_scope :by_status, :by_tasktype, :by_taskid,  :by_hostname, :by_clecid

  def index
    @task_queues = apply_scopes(TaskQueue).all
    @task_queues = @task_queues.paginate(:page => params[:page], :per_page => 30)
  end
end

编辑: 这是一张图片,可以更好地展示正在发生的事情:http://i.imgur.com/SQZZDXC.png 我会将其显示为图像,但还没有足够的代表。

最佳答案

ActiveRecord 提供了uniq 方法来生成SELECT DISTINCT ... 查询。例如,你可以做

@clec_ids = apply_scopes(TaskQueue).uniq.pluck(:clecid)

如果你想重新使用已经选择的@task_queues,你可以使用表亲方法Array#uniq来确保唯一的clecid显示

@clec_ids = @task_queues.map(:clecid).uniq

在你看来你会这样做

<select name="by_clecid">
  <option value="">All</option>
  <% @clec_ids do |clec_id| %>
    <option value="<%= clec_id %>"><%= clec_id %></option>
  <% end %> 
</select>

关于html - 无法弄清楚如何将结果限制为希望使用 .group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613382/

相关文章:

html - 下拉菜单不适用于 IFrame?

javascript - 我如何使网站无响应?

javascript - 在滚动视口(viewport)中按顺序动画元素

ruby-on-rails - Heroku 应用程序上的远程 mysql 数据库

ruby-on-rails - 您如何使用 rspec 测试 omni-auth facebook?

html - 在 Ruby 中有条件地添加属性的内联方法吗?

javascript - 在移动设备上禁用粘性导航

ruby-on-rails - 如何测试Rails的ETag缓存?

ruby - 将排序的 Ruby 数组转换为具有可能重复的排名

ruby - Ruby 中的方法同义词