Find all entries created on a specific day of the week

I need to find all records created on a specific day of the week. I only have a standard date available to me. How do I do this in activerecord?

+3
source share
3 answers

You can use the DAYOFWEEK function in MySQL and pass it to the: conditions parameter. Suppose you have a model called Item, this will return all items created on Sunday:

Item.all (: conditions => ['dayofweek (created_at) =?', 1])

Using Postgres, you can do something similar with to_char .

, , , , MySQL . , , , .

+2

where("extract(dow from created_at) = ?", Date.today.wday)

, postgres. , , . , , , .

+3

Unix Epoch. Time.to_i Ruby.

7 ( 0 6).

dayOfWeek = (epochseconds / 86400 ) % 7;
+1

Source: https://habr.com/ru/post/1776730/


All Articles