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