【发布时间】:2016-05-26 03:14:23
【问题描述】:
我正在尝试编写查询,但找不到正确的结果。我有一个这样的模型:
#<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">
它是一个跟踪器,所以我有多个资源,如果我返回同一页面,相同的资源会被跟踪很多时间。
我想知道用户阅读了多少新闻,所以我尝试:
Ahoy::Event.where(user_id:3).group(:properties["resource:news"]).count
更新
我也在尝试
Ahoy::Event.where(user_id:3).where("properties @> (:key => :value)", :key => 'resource', :value => 'Gate').count
** 更新 2 ** 另一个暂定:
Ahoy::Event.where(user_id:3).where("properties --> 'resource': 'Gate'").select("properties #>> 'id' as resource, count('resource')").group("resource")
【问题讨论】:
-
properties列是什么数据库类型? -
@SimoneCarletti 这是一个 jsonb
-
JSONB 具有特定的查询语法。你将不得不使用它。我不记得它到底是什么,但stackoverflow.com/questions/30519458/…你可能有一些提示。我不确定您是否可以明确地
GROUP BYjsonb 字段值。
标签: ruby-on-rails activerecord