2012-08-07 80 views
0

我的應用程序允許用戶搜索許多不同的字段和日期以查找項目記錄。目前,當您選擇某個時間尺度,即(從現在開始1年),搜索將正常返回。當您再次使用相同時間標度和某個客戶端進行搜索時,搜索將再次正常返回。當我試圖在客戶端上進行搜索時,問題在於。搜索沒有返回結果,這是錯誤的。我敢肯定它是與我的運營商在搜索方法:Ruby on Rails:運營商

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

希望有人能看到它。提前致謝。只是爲了讓你知道我是一個小白導軌以及:)

編輯:

這裏是我的搜索視圖,讓您瞭解搜索如何工作:

<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 %> 
+2

有一些小技巧,當你想解決這樣的問題,火了IRB。在那裏測試你的假設,特別是當你有一個複雜的查詢。如果它有效,那麼模型是好的,那麼你可以檢查你是否能夠從視圖中傳遞變量。另外,看看範圍,它會讓你的代碼更具可讀性。 – Pran 2012-08-07 15:10:28

+0

我剛剛開始學習ROR。我需要學習如何使用IRB,我認爲。謝謝你的提示 – Jazz 2012-08-07 15:12:49

回答

0

固定我自己的問題:/

我只是增加了一個else

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 


if (search_start_date_A.blank? or search_start_date_B.blank?) and search_start_date_dd.blank? 

where(['client LIKE ? AND keywords LIKE ?', 
     "%#{search_client}%", "%#{search_keywords}%" 
    ]) 

else 

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 
end