ユーザーにタイムスケールで検索するオプションを提供しようとしています。次のようなドロップダウンメニューにオプションを表示するドロップダウンメニューが必要です。
検索: 3 日前、1 週間前、1 か月前、6 か月前、1 年。など。
Railsが3日前にクエリを実行したいことを理解するために使用する必要があることは理解していDateTime.now.to_date - 3.days
ますが、ユーザーに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