2014-07-17 25 views
4

我有一個洗劫搜索表單:Rails 4 + Ransack:lteq與datetime無法正常工作?

<h1>Scheduled Payments</h1> 


<%= search_form_for [:admin, @search] do |form| %> 
    <%= form.text_field :start_date_gteq, value: @order_date_gteq_with_proper_format, class: "datepicker" %> to 
    <%= form.text_field :start_date_lteq, value: @order_date_lteq_with_proper_format, class: "datepicker" %> 
    <%= form.submit "Search" %> 
<% end %> 

我希望用戶能在2個日期類型,併爲每ScheduledPayment紀錄起始日期大於或等於1文本字段,比起始日期小於或等於第二個文本字段出現。

這工作:

enter image description here

注意該記錄有07/17/2014的開始日期。當我輸入第二個文本字段07/17/2014,什麼也不顯示:

enter image description here

我猜這是因爲在該記錄的起始日期的小時或分鐘大於+00:00:00,這可能是用作第二個字段的默認值。在這種情況下,我將如何抓取所有記錄,並使用start_date 07/01/201407/17/2014 23:59:5907/17/2014的絕對末端)?

回答

2

你可以試試以下嗎?

# the controller's action where you pass the params to the search method: 
def your_action 
    start_time = params[:start_date_gteq].to_date rescue Date.current 
    start_time = start_time.beginning_of_day # sets to 00:00:00 
    end_time = params[:start_date_lteq].to_date rescue Date.current 
    end_time = end_time.end_of_day # sets to 23:59:59 
    ScheduledPayment.search(start_time: start_time..end_time) 
+0

謝謝Yoshi。您幫助我認識到,只需使用純SQL/ActiveRecord就可以實現這一目標,而不需要ransack。 – Edmund