user3741713 user3741713 - 1 year ago 96
SQL Question

rails convert datetime to date within a scope

In my model I defined the following scope:

scope :today, -> { where time_start: }

This is not working because I'm comparing a mysql datetime against a date.
I am trying to do the equivalent of the date() mysql function (extracting a date from a datetime).

Answer Source

Try this:

scope :today, -> { where time_start: }

Using range in criteria will generate a BETWEEN query, something like this:

SELECT "models".* FROM "models"
    WHERE (
        "models"."time_start" BETWEEN
            '2016-07-29 00:00:00.000000' AND '2016-07-29 23:59:59.999999'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download