user984621 user984621 - 1 month ago 12
SQL Question

Rails ActiveRecord - how to fetch records between two dates

I have in the database two date columns -

from_date
and
to_date
.

Example:


  • from_date
    : 2012-09-10

  • to_date
    : 2012-09-30

  • today
    : 2012-09-13



I would need to fetch all records, if
today
's date is between
from_date
and
to_date
. How to do that in SQL query?

If I have loaded the respective record, I can easily decide, if today's date is between
from_date
and
to_date
, but I don't know, how to fetch straight those record from database.

Answer
data = ModelName.where("today >= from_date AND today <= to_date")