我想創建一個下拉列表中篩選結果。到目前爲止,除了多個項目正在列出之外,所有工作都在進行。無法弄清楚如何限制結果爲一個有希望使用.group
如:
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>
我以爲我可以使用。集團,但不完全知道如何使用它。任何幫助將不勝感激我不熟悉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
有些控制器:
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 我會展示它作爲一個形象,但沒有足夠的代表呢。
這是因爲它是從數據庫中拉出的HTML Clec ID列表216兩次以上,我只是以此爲例。我正在尋找一種使用ruby的方法,只有在它不存在的情況下才能使用它。從我所知道的方法.group應該可以工作,但我不完全確定。 –
@ArupRakshit他寫道,一個是選項值,另一個是在選項列表中吐出的實際文本。 – emcanes
@emcanes有道理! :) –