我正在尝试为用户提供在时间刻度上搜索的选项。我想要一个下拉菜单,在下拉菜单中显示选项,如下所示:
搜索自:3天前,1周前,1个月前,6个月前,1年。等。
我知道我必须使用DateTime.now.to_date - 3.days
所以rails知道我想在3天前查询,但我不确定如何让用户将3天前的选项视为文本在下拉列表中,但查询DateTime.now.to_date - 3.days
由于
我的搜索视图目前看起来像这样:
<h1>Search</h1>
<% if @project_search.total_entries > 0 %>
<%= form_tag search_path, method: :get do %>
Client :
<%= select(@projects, :client, Project.all.map {|p| [p.client]}.uniq, :prompt => "-Any-", :selected => params[:client]) %></br>
Industry :
<%= select(@projects, :industry, Project.all.map {|p| [p.industry]}.uniq, :prompt => "-Any-", :selected => params[:industry]) %></br>
Role :
<%= select(@projects, :role, Project.all.map {|p| [p.role]}.uniq, :prompt => "-Any-", :selected => params[:role]) %></br>
Technologies :
<%= select(@projects, :tech, Project.all.map {|p| [p.tech]}.uniq, :prompt => "-Any-", :selected => params[:tech]) %></br>
Business Division :
<%= select(@projects, :business_div, Project.all.map {|p| [p.business_div]}.uniq, :prompt => "-Any-", :selected => params[:business_div]) %></br>
Project Owner :
<%= select(@projects, :project_owner, Project.all.map {|p| [p.project_owner]}.uniq, :prompt => "-Any-", :selected => params[:project_owner]) %></br>
Exception PM
<%= select(@projects, :exception_pm, Project.all.map {|p| [p.exception_pm]}.uniq, :prompt => "-Any-", :selected => params[:exception_pm]) %></br>
<%= select_tag "start_date", options_for_select({
"3 days ago" => 3.days, # = 259_200 sec.
"1 week ago" => 1.week, # = 604_800 sec.
"1 month ago" => 1.month, # = 2_592_000 sec.
"6 months ago" => 6.months, # = 15_552_000 sec.
"1 year ago" => 1.year, # = 31_557_600 sec.
}) %>
Project Dates between
<%#= text_field_tag("start_date") %>
and
<%= text_field_tag("end_date") %></br>
Status :
<%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt => "-Any-", :selected => params[:status]) %></br>
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
<%= submit_tag "Search", name: nil %>
<% end %>
我的模特:
class Project < ActiveRecord::Base
attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech
validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech
def self.search(search_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date, search_end_date, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date.present? || search_end_date.present? || search_keywords.present?
where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND DATE(start_date) BETWEEN ? AND ? AND DATE(end_date) BETWEEN ? AND ? AND keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" ,
"%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%",
search_start_date, search_end_date, search_start_date, search_end_date,"%#{search_keywords}%"
])
end
def self.paginated_for_index(projects_per_page, current_page)
paginate(:per_page => projects_per_page, :page => current_page)
end
end
答案 0 :(得分:1)
您可以以秒为单位指定时间范围:
<%= select_tag "ago", options_for_select({
"3 days ago" => 3.days, # = 259_200 sec.
"1 week ago" => 1.week, # = 604_800 sec.
"1 month ago" => 1.month, # = 2_592_000 sec.
"6 months ago" => 6.months, # = 15_552_000 sec.
"1 year ago" => 1.year, # = 31_557_600 sec.
}) %>
在查询中使用该值:
Model.where("created_at >", Time.now - params[:ago])
答案 1 :(得分:1)
使用由github人写的chronic gem支持的自然语言处理。从用户获得人类可读格式的范围&amp;然后使用chronic
将它们解析为ruby理解的完全有效日期,并且可以作为参数传递给Active Record以查询数据库
这里有一些例子:
1.9.3p194 :001 > require 'chronic'
=> true
1.9.3p194 :002 > Chronic.parse '3 days ago'
=> 2012-07-31 15:20:59 +0530
1.9.3p194 :003 > Chronic.parse '1 week ago'
=> 2012-07-27 15:22:15 +0530
1.9.3p194 :004 > Chronic.parse '1 month ago'
=> 2012-07-03 15:22:26 +0530
1.9.3p194 :005 > Chronic.parse '6 months ago'
=> 2012-02-03 15:23:25 +0530
1.9.3p194 :006 > Chronic.parse '1 year ago'
=> 2011-08-03 15:23:34 +0530
现在,你也可以在这里获得一些优点,也可以做一些像
这样的事情1.9.3p194 :007 > Chronic.parse 'last night'
=> 2012-08-02 22:00:00 +0530
README中提供了更多示例,但由于其非常灵活,我会说您在接受用户输入时也可以获得创意。因此,您可以允许用户选择任意日期,而不是选择框中的预定值(您仍可以执行此操作)。我知道这不是传统方式,但我认为使用NLP非常酷!所以try it