LoginSignup
11
7

More than 5 years have passed since last update.

Ransackで日付型のFrom-To検索をする

Posted at

ちょっとやり方がわからなかったので。
GithubのIssueに書いてありました。

Create a 'between' query with ransack?
https://github.com/activerecord-hackery/ransack/issues/101

View

hoge.html.erb
<%= f.label :created_at_gteq %>
<%= f.text_field :created_at_gteq %>

<%= f.label :created_at_lteq %>
<%= f.text_field :created_at_lteq %>

Console

Item.search(:created_at_gteq => "2014-09-10", :created_at_lteq => "2014-09-25").result.to_sql

=> "SELECT `items`.* 
FROM `items`  
WHERE ((`items`.`created_at` >= '2014-09-10 00:00:00' 
AND `items`.`created_at` <= '2014-09-25 00:00:00'))"
11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7