开发者

strange duplicate on group

What can be wrong ? Any help is appreciated.

@entries = Entry.where(:created_at => 3.day.ago.beginning_of_day..开发者_运维知识库Time.now.end_of_day).group("date(created_at)").select("created_at, count(id) as tcount")

<% @entries.each_with_index do |entry, index| %>
<%= entry.created_at.strftime("%d/%m") %>
<% end %>

Output:

16/07
16/07
18/07
19/07


When you select the records, it is grouping by the database's date() function, which uses what is actually stored in the database, but you are selecting the complete created_at, including time. It chooses the first one as the value. Timezone conversions are then coming into play when you actually display it. I bet one of those 16's should be a 15.

Try:

@entries = Entry.where(:created_at => 3.day.ago.beginning_of_day..Time.now.end_of_day).group("date(created_at)").select("date(created_at) as date, count(id) as tcount")

<% @entries.each_with_index do |entry, index| %>
<%= entry.date.strftime("%d/%m") %>
<% end %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜