2

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?

user3141095
  • 129
  • 1
  • 11
  • Is it feasible to remove the double quotes? – user10089632 Dec 02 '17 at 05:55
  • no that yields a syntax error: `syntax error, unexpected tSTRING_BEG, expecting keyword_do_LAMBDA or tLAMBEG _of_month).top(properties -> 'value', 5, nil: false)` – user3141095 Dec 02 '17 at 06:09
  • did you run `rails generate ahoy:stores:active_record`? This is something specific from `postgresql` could be useful including the sql generated from the server and all the details and the sql tags...https://www.postgresql.org/message-id/alpine.LRH.2.00.1108310820490.21634@rray.drdc.mstc.ms.gov – Fabrizio Bertoglio Dec 02 '17 at 10:35
  • Yes the generator was run post install. – user3141095 Dec 02 '17 at 15:55

0 Answers0