如何按小时创建今天的记录?

时间:2016-10-14 22:29:48

标签: ruby-on-rails sqlite ruby-on-rails-4 morris.js

我正在使用Rails 4.2,SQLite和Morris.js来创建有关销售变动的图表和统计信息。我正在尝试创建一个折线图,显示按小时分组的订单总数。就像上午9点到10点之间,我们卖出了许多X美元的订单

应用程序/视图/ static_pages / stats.html.erb

  <div class="col-sm-12">
    <h4>Today:</h4>
    <%= content_tag :div, "", id: "orders_tdh_chart", data: { orders: orders_tdh_chart } %>
  </div>

staic_pages_helper.rb

  def orders_tdh_chart
    Order.where("created_at >= ?", Date.today.beginning_of_day).group("strftime('%H', created_at)").sum(:total_after_discount)
  end

static_pages.coffee

  Morris.line
    element: 'orders_tdh_chart'
    data: $('#orders_tdh_chart').data('orders')
    xkey: 'created_at'
    ykeys: [ 'total_after_discount' ]
    labels: [ 'Total' ]

图形返回空,并且视图中的所有其他图形在工作后返回空白。同样在控制台中生成的查询返回非常奇怪,如下所示...

   ...
   (0.3ms)  SELECT SUM("orders"."total_after_discount") AS sum_total_after_discount, strftime('%H', created_at) AS strftime_h_created_at FROM "orders" WHERE (created_at >= '2016-10-14 00:00:00.000000') GROUP BY strftime('%H', created_at)
   (0.2ms)  SELECT SUM("orders"."total_after_discount") FROM "orders" WHERE (date(created_at) = '2016-09-17')
   (0.2ms)  SELECT SUM("orders"."total_after_discount") FROM "orders" WHERE (date(created_at) = '2016-09-18')
   (0.2ms)  SELECT SUM("orders"."total_after_discount") FROM "orders" WHERE (date(created_at) = '2016-09-19')
   ...

0 个答案:

没有答案