I am trying to get the last 5 days that an object has. Currently my code is similar to this
Post.uniq('performed_at').ascending.last(5).group_by{|p| p.performed_at}
but I get 4 dates instead of 5, and I think because there are 2 records of the last 5 that were created on the same day. How can I get the last 5 days when a message has a record?
You can do:
Post.select('DISTINCT created_at').order('created_at DESC').limit(5).pluck(:created_at)
Assuming you just need the dates themselves, not the unique dates, this would be better:
Post.order('created_at desc').uniq.limit(5).pluck('DATE(created_at)')
This will only return unique calendar dates without any specific dates.
. , .
Post.select('performed_at').uniq.order('performed_at DESC').limit(5).pluck(:performed_at).reverse
I also added reverse, since I want the array of days to return to me in chronological order. I also had uniq () since select ('DISTINCT perform_at') still returned duplicate dates.
Hope this helps.
Source: https://habr.com/ru/post/1532426/More articles:имя переменной коллекции в оболочке mongodb с помощью javascript - javascriptSVGSVGElement.children not working in IE11? - domCopy Cyrillic URLs in Google Chrome? - urlUITableViewCell separatorInsets in iOS 7 - uitableviewявляется ли по умолчанию constuctor объявлением, является обязательным? - javaCancel TFS - c #inappbrowser not working properly on iOS - iosAny way to place the app in the background and restart in Appium iOS - iosTracking changes in python source files? - pythonInvalid value PLAY_LANG - javaAll Articles