我的应用程序允许用户搜索许多不同的字段和日期以查找项目记录。目前,当您选择某个时间尺度时,即(从现在开始的 1 年),搜索会正确返回。当您使用相同的时间尺度和某些客户端再次搜索时,搜索会再次正确返回。当我尝试仅在客户端上搜索时,问题就出在此处。搜索没有返回结果,这是错误的。我确定这与我在搜索方法中的运算符有关:
def self.search(search_client, search_start_date_dd, search_start_date_A, search_start_date_B, search_keywords)
return scoped unless search_client.present? || search_start_date_dd.present? || search_start_date_A.present? || search_start_date_B.present? || search_keywords.present?
todays_date = DateTime.now.to_date
if !search_start_date_A.blank? or !search_start_date_B.blank?
search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
todays_date = ""
end
if search_start_date_dd.blank?
todays_date = ""
end
where(['client LIKE ? AND (DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ?) AND keywords LIKE ?',
"%#{search_client}%", search_start_date_dd, todays_date , search_start_date_A, search_start_date_B, "%#{search_keywords}%"
])
end
我确定问题出在'client LIKE ? AND (DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ?) AND keywords LIKE ?'
希望有人能看到。提前致谢。只是为了让你知道我也是一个 Rails 菜鸟 :)
编辑:
这是我的搜索视图,因此您了解搜索的工作原理:
<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>
Select start dates from:
<%= 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] )%>
until now <%= l DateTime.now.to_date %>, or select project start dates between
<%= text_field_tag :start_date_A, params[:start_date_A] %>
and
<%= text_field_tag :start_date_B, params[:start_date_B] %>
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
<%= submit_tag "Search", name: nil %>