Yshmarov
Yshmarov

Reputation: 3749

Ruby: count Post quantity per day

I have: scaffold post title:string body:text

How can I display post quantity created per day (based on created_at:datetime)?

I figure out it will contain something like

= Post.count(:id)#daily...

How can I show it in a table like this?

%table
  %thead
    %tr
      %th Date
      %th Post quantity
  %tbody
    - @posts.each do |post|
      %tr
        %td= post.date
        %td= post.quantity

Upvotes: 0

Views: 96

Answers (2)

T J
T J

Reputation: 1342

This will give you a hash of dates to count

Post.group("DATE(created_at)").count

To display in your table

- Post.group("DATE(created_at)").count.each do |date, post_count|
  %tr
    %td= date
    %td= post_count

Upvotes: 3

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

For posts that were created today:

Post.where("DATE(`value_date`) = CURDATE()").count

While I think that answer given by @t-j is more generic, the above is more lightweight and is executed times faster.

Upvotes: 0

Related Questions