I am trying to find the proper way to get the top 5 (by count) events in a database based on a particular property in the events ("value"). I am also using the hightop gem to make it easier to get this top 5.
If I use:
Ahoy::Event.where(time: Date.today.beginning_of_month..Date.today.end_of_month).top(:name, 5)
This works, but sorts by event name. I would like to sort by the "value" property which lives in that event. Here's what a typical event looks like:
<Ahoy::Event id: 229, visit_id: 318, user_id: 1, name: "Thing", properties: {"episode"=>"1", "title"=>"Test", "value"=>"Thing Ep 1: Test"}, time: "2017-11-02 11:34:10">
I have tried to do:
Ahoy::Event.where(time: Date.today.beginning_of_month..Date.today.end_of_month).top("properties['value']", 5)
as well as:
Ahoy::Event.where(time: Date.today.beginning_of_month..Date.today.end_of_month).top("properties ->> 'value'", 5)
but this yields the following error: "cannot subscript type text because it is not an array". Is there a way to actually do what I want?