在我的 Rails 5 应用程序中,我使用了Ahoy gemproperties
,它使用我不知道如何查询的 jsonb 字段(称为)。
properties
字段中的数据示例:
"{\"id\":\"11\"}"
我想做的是:
- 查找过去 7 天内创建的记录(使用该
time
字段) id
按properties
字段中的记录分组- 根据记录
id
最多的记录排序
我试过这个:
@foo = Ahoy::Event.where(name: "Viewed Product").where(time: 7.days.ago..Time.now).group("properties(id)").order('COUNT("properties(id)") DESC')
但我收到了错误,PG::UndefinedColumn: ERROR: column "properties(id)" doe not exist
我在 gem 的问题中发现了这个有点相似的问题,所以我尝试将查询分成几部分:
@foo = Ahoy::Event.where(name: "Viewed Product").where(time: 7.days.ago..Time.now)
@foo.group("properties REGEXP '[{,]\"id\"...).count
但后来我的 IDE 给出了错误unterminated string meets the end of file
。
有人可以帮我确定我做错了什么吗?