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.
This commit is contained in:
Javi Martín
2024-05-17 15:36:52 +02:00
parent 1d85a63e7c
commit c92cb6bed1

View File

@@ -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