From c92cb6bed16fc41e06316b6a7de6733f055075ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 17 May 2024 15:36:52 +0200 Subject: [PATCH] Use a range in the between_ages user scope Using `>` and `<` for dates is a bit confusing because it usually requires mentally translating `<` into "before" and `>` into "after", meaning it takes a few seconds to check which is the starting date and which is the ending date. When using a range, it's easier to see which is which. Note that we were using `>` and `<`, but now we're using the equivalent of `>=` and `<=`. This makes sense IMHO, since the beginning of the year and the end of the year should also be included in this case. --- app/models/user.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index b5a70b075..5421b56ce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -120,11 +120,7 @@ class User < ApplicationRecord where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", search, search, search) end scope :between_ages, ->(from, to, at_time: Time.current) do - where( - "date_of_birth > ? AND date_of_birth < ?", - (at_time - to.years).beginning_of_year, - (at_time - from.years).end_of_year - ) + where(date_of_birth: (at_time - to.years).beginning_of_year..(at_time - from.years).end_of_year) end before_validation :clean_document_number