开发者

Rails/SQl query help: Find all by created_at in past 7 days per each day?

开发者 https://www.devze.com 2022-12-31 23:59 出处:网络
I\'m unable to get SQL and Rails to play properly when trying to find Categories that are created each day, the past 7 days.

I'm unable to get SQL and Rails to play properly when trying to find Categories that are created each day, the past 7 days.

So basically I want to find each开发者_如何学JAVA Category sorted by the day they were created for the past 7 days.

I found this on stackoverflow, but it isn't finding a Category that I just created:

Category.all(:conditions => ["created_at > ? AND created_at < ?", t.at_beginning_of_day, t.tomorrow.at_beginning_of_day])

Any help?


Turns out it was UTC time difference.

range = "created_at #{(5.days.ago.utc...Time.now.utc).to_s(:db)}"
Category.all(:conditions => range)

That did the trick. Can anyone tell me if there is a better way to do it?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号