0

I'm trying to write a query and I cannot find the right result. I have a model like this:

#<Ahoy::Event id: "007d74fb-6225-4cd5-a725-6e35b2e9a859", visit_id: "34713617-d6f6-4adb-b436-8029a7ea586c", user_id: 3, name: "view", properties: {"id"=>14, "delay"=>500, "action"=>"show", "resource"=>"News", "end_point"=>"news"}, time: "2016-02-12 15:13:05">

It's a tracker, so I have multiple resources and the same resource is tracked a lot of time if I return on the same page.

I want to know how many news the user read, so I try with:

Ahoy::Event.where(user_id:3).group(:properties["resource:news"]).count

Update

I'm also trying with

Ahoy::Event.where(user_id:3).where("properties @> (:key => :value)", :key => 'resource', :value => 'Gate').count

** Update 2 ** Another tentative:

Ahoy::Event.where(user_id:3).where("properties --> 'resource': 'Gate'").select("properties #>> 'id' as resource, count('resource')").group("resource")
Roberto Pezzali
  • 2,484
  • 2
  • 27
  • 56

1 Answers1

0

I've tried on my console in psql, it worked.

Maybe you are missing the curly brackets?

Ahoy::Event.where(user_id:3).
  where("properties @> ({:key => :value})", :key => 'resource', :value => 'Gate').
  count
devanand
  • 5,116
  • 2
  • 20
  • 19
  • I notice that I do a mistake. I must group also by properties[:id] and I miss that part. I'm getting crazy with this query... I don't know how to group by properties id. – Roberto Pezzali Feb 15 '16 at 10:27