Ruby on Rails:搜索时的优先级字段

时间:2012-08-06 08:59:26

标签: ruby-on-rails ruby search drop-down-menu

我正在尝试创建一个搜索页面,用户可以在其中搜索许多不同字段的项目记录。我希望他们能够在clientindustrystart_dateend_datekeywords上进行搜索。

目前,该页面显示“客户端”和“行业”的下拉菜单,以及“关键字”的文本框,因此用户可以从数据库中进行选择并搜索关键字。

我在搜索开始日期时遇到问题,因为我试图让用户选择在某个时间范围内搜索“开始日期”,即3天前,1周前......在下拉列表中菜单,或者通过使用jquerys datepicker放入“开始日期”和“结束日期”来搜索特定日期。

我希望它将特定日期作为优先级,因此用户可以同时选择时间刻度和特定日期。

这是我的搜索视图:

    <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>    

Select start date:
<%= select_tag "start_date_dd", options_for_select({
    "Select a period" => "",
      "3 days ago"   => DateTime.now.to_date - 3.days,   # =    259_200 sec.
      "1 week ago"   => DateTime.now.to_date - 1.week,   # =    604_800 sec.
      "1 month ago"  => DateTime.now.to_date - 1.month,  # =  2_592_000 sec.
      "6 months ago" => DateTime.now.to_date - 6.months, # = 15_552_000 sec.
      "1 year ago"   => DateTime.now.to_date - 1.year,   # = 31_557_600 sec.
    }, :selected=>params[:start_date_dd] )%>    

or select project dates between

<%=  text_field_tag ("start_date") %>

and

<%= text_field_tag("end_date") %></br>    

Keywords :

<%= text_field_tag :keywords, params[:keywords] %></br>

<%= submit_tag "Search", name: nil %>

<% end %>

这是我的项目模型在搜索中没有特定日期选项的情况下工作:

class Project < ActiveRecord::Base
  attr_accessible :client, :end_date, :industry, :keywords, :start_date, 


def self.search(search_client, search_industry, search_start_date_dd,  search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present?  || search_keywords.present?

  where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ?  AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" ,  
      search_start_date_dd, DateTime.now.to_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

这是我的项目控制器的搜索操作,它只考虑了时间刻度搜索。

def search

@search = params[:client], params[:industry], params[:start_date_dd],  params[:keywords]

@project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)

@search_performed = !@search.reject! { |c| c.blank? }.empty? 

   @project = Project.new(params[:project])

respond_to do |format|
      format.html # search.html.erb
      format.json { render :json => @project }
    end

end

我曾尝试包含具体的日期搜索,但没有运气。希望有人可以指出他们正确的方向。我是一个铁杆菜鸟,所以请放轻松我。提前谢谢。

编辑:这是我试图让它发挥作用。

def self.search(search_client, search_industry, search_start_date_dd, search_start_date, search_end_date, search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present? || search_start_date.present? || search_end_date.present? || search_keywords.present?

where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ? AND DATE(end_date) BETWEEN ? AND ? AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" , search_start_date_dd, DateTime.now.to_date, search_start_date, search_end_date, search_start_date, search_end_date,"%#{search_keywords}%"
    ])

end

1 个答案:

答案 0 :(得分:1)

我会修改start_date_dd下拉列表以获得自定义字段,然后添加jQuery以显示/隐藏start_date / end_date字段。然后在你的控制器中,你可以查看start_date_dd是否是自定义字段,然后你应该使用start_date / end_date字段。

其他评论:

我不会将他们搜索的内容直接传递给数据库。我会先运行以下内容:

user_supplied_start_date = Time.zone.parse(search_start_date)

这样,如果他们给你一个无效的日期(解析了user_supplied_start_date == nil),你可以给他们一个警告,你无法解析search_start_date并向他们显示最后的 _ _天

我还看到了以下代码:

def self.search
  scoped = scoped.where(['client like %?%', params[:client]) if params[:client].present?
  scoped = scoped.where(['client like %?%', params[:industry]) if params[:industry].present?
  ...
  scoped
end

这样你就会根据他们发给你的东西进行搜索。

您只列出当前分配给项目的客户端。用户是否应该能够搜索所有客户并看到 _ 客户端没有任何项目?

作为一般规则,我更喜欢帮助方法在视图中使用代码......

Project.all.map {|p| [p.client]}.uniq

VS

# app/helpers/search_helper.rb
def clients_for_select
  Client.select("id, name").map{|c| [c.id, c.name]}
end