[RUBY] How to write a date comparison search in Rails

Ruby2.7.1 Rails6.0.3.3 Confirm with

I wrote a DB search process in Rails for the first time in a long time, and I was investigating how to write a date comparison search, so I wrote a memo. Most of the time, when I was looking for something else like writing SQL or using Arel, I could write it as follows.

[0] pry(main)> User.where(created_at: ..DateTime.now)
  User Load (0.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`created_at` <= '2020-10-27 14:41:26.943067'
[
    [0] #<User:0x000055861898f208> {
                   :id => 1,
           :created_at => Tue, 27 Oct 2020 14:18:54 JST +09:00,
           :updated_at => Tue, 27 Oct 2020 14:18:54 JST +09:00,
    }
]

The opposite is

 User.where(created_at: DateTime.now..)
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`created_at` >= '2020-10-27 14:52:57.534880'

It feels like.

Recommended Posts

How to write a date comparison search in Rails
[Rails] How to write in Japanese
How to write a migration from Rails datetime type to date type
How to implement search functionality in Rails
How to write Rails
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to make a follow function in Rails
How to write Rails validation
How to write Rails seed
How to write Rails routing
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
[How to insert a video in haml with Rails]
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
Add a search function in Rails.
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to introduce jQuery in Rails 6
How to install Swiper in Rails
How to select a specified date by code in FSCalendar
How to display a graph in Ruby on Rails (LazyHighChart)
Establish a search bar in Rails ~ Join multiple tables to search
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
How to implement date calculation in Java
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
[Basic] How to write a Dockerfile Self-learning ②
[Introduction to Java] How to write a Java program
How to use MySQL in Rails tutorial
How to fix system date in JUnit
Steps to set a favicon in Rails
[rails] How to configure routing in resources
How to get the date in java
How to rename a model with foreign key constraints in Rails
[rails] How to create a partial template
How to implement ranking functionality in Rails
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
How to publish a library in jCenter
How to use credentials.yml.enc introduced in Rails 5.2
[SpringBoot] How to write a controller test
How to write the view when Vue is introduced in Rails?
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to log in with a name by adding a devise name column
How to update user edits in Rails Devise without entering a password
How to get the current date as a string in yyyyMMdd format
Convert to a tag to URL string in Rails
[JavaFX] How to write Eclipse permissions in build.gradle
[Rails] Various ways to write in seed files
[Rails] How to create a graph using lazy_high_charts
How to display a web page in Java
[Rails] How to use select boxes in Ransack
JUnit 5: How to write test cases in enum