2

我一直在尝试执行以下所有可能的方法:

@events.group(:name).count

在我的 index.html.erb 页面中使用 chartkick 显示事件总数。我不断遇到诸如 nil:method 和 each_pair 未知方法之类的错误。组或计数的 mongoid 等价物是什么?我努力了:

@events.only(:name).group 
@events.where(name: @events.name).aggregate
@events.only(:name).aggregate
@events.only(:name).group_by(&:_id)

不知道什么会起作用......任何帮助将不胜感激。我的控制器如下:

class EventsController < ApplicationController

  before_filter :authenticate_user!

  def index
    @domain = Domain.find_by(id: params[:domain_id])
    @events = @domain.events
  end
end

我的看法如下:

<h1>Events</h1>


<%= line_chart @events.all.aggregrate %>

<%= line_chart @events.group_by_day(&:created_at).count %>

我正在使用带有 Mongoid 4.0 和 Chartkick 的 Rails 4。

4

1 回答 1

4

我找到了答案……基本上我不得不使用 mongoDB 查询……不知道为什么……但这就是我所做的

<%= line_chart Event.collection.aggregate("$group" => { "_id" => "$name", count: {"$sum" =>  1} } )%>

<%= line_chart Event.collection.aggregate({"$group" => {"_id" => {"name" => "$name", "created_at" => "$created_at"}}}, {"$group" => {"_id" => "$_id", count: {"$sum" => 1 } } } )%>
于 2014-07-19T01:05:05.913 回答