2

I have been trying every possible way of executing the following:

@events.group(:name).count

in my index.html.erb page using chartkick to display the total number of events. I keep running into errors like nil:method and each_pair unknown method. What would be the mongoid equivalent of a group or count? I have tried:

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

Not sure what would work...any help will be appreciated. My controller is the following:

class EventsController < ApplicationController

  before_filter :authenticate_user!

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

My view is the following:

<h1>Events</h1>


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

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

I am using Rails 4 with Mongoid 4.0 and Chartkick.

ShaunK
  • 1,181
  • 5
  • 22
  • 41

1 Answers1

4

I found the answer to this...essentially I had to use mongoDB queries...not sure why..but this is what I did

<%= 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 } } } )%>
ShaunK
  • 1,181
  • 5
  • 22
  • 41