I have the date when an article was created in a DB table. I have articles since the August until today.
I tried to create a SQL query, in ActiveRecord, to get the articles that were created today.
For example, today is the 14th of January and I tried to get those articles that were created on the 14th of August, 14th of September until today.
I tried something like this:
today = Date.today
day = today.strftime('%d')
day_next = (today + 1.day).strftime('%d')
Article.where('created_at >= ? AND created_at <= ?', day, day_next) #day = 14, day_next = 15
But this doesn’t return the right statement, because articles were released on the 14th of December and the 14th of October. This query returns me nothing.
You’re overthinking this. Assuming
created_atis a normal datetime/timestamp field you should just convertcreated_atto its day usingDAY()and then compare it directly to a normal Ruby Date object.(Misread your question originally; answer updated.)