1

在我的 Rails 5 应用程序中,我使用了Ahoy gemproperties ,它使用我不知道如何查询的 jsonb 字段(称为)。

properties字段中的数据示例:

"{\"id\":\"11\"}"

我想做的是:

  1. 查找过去 7 天内创建的记录(使用该time字段)
  2. idproperties字段中的记录分组
  3. 根据记录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

有人可以帮我确定我做错了什么吗?

4

1 回答 1

0

如果您使用的是 PostgreSQL,我相信您将不得不像这样引用 json 列: .group("properties ->> 'id'")

于 2017-03-01T05:43:42.297 回答