我正在使用 Ahoy gem 在 Rails 4 应用程序中记录用户操作。事件被记录并存储在 Ahoy::Event 表中。每个事件都有一个properties
以文本形式存储的哈希属性。在这种情况下,我试图返回特定文章的文章浏览次数。
我在此处的 gem 文档中遵循建议的语法:https ://github.com/ankane/ahoy#querying-properties ,它提供了一个Ahoy::Event.where("properties LIKE '%\"store_id\": \"1\"%'").count
查询属性文本哈希的示例,但我无法返回准确的结果。
这是我正在跟踪以进行测试的控制器事件:
def show
@article = Article.friendly.find(params[:id])
...
ahoy.track "Viewed article", article_id: @article.id
...
end
在控制台中,正如预期的那样,当我搜索名称为“已查看文章”的文章时,我可以看到返回了一个事件:
Ahoy::Event.where(name: "Viewed article")
Ahoy::Event Load (89.1ms) SELECT "ahoy_events".* FROM "ahoy_events" WHERE "ahoy_events"."name" = $1 [["name", "Viewed article"]]
=> #<ActiveRecord::Relation [#<Ahoy::Event id: "6cda3790-625d-4b73-8ef0-262a26b29618", visit_id: "2d56efa6-3590-4a64-acec-c0fd0ba07727", user_id: nil, name: "Viewed article", properties: {"article_id"=>562}, time: "2016-06-03 18:49:28">]>
但是当我尝试通过查询具有相应 article_id 的事件来查找相同的事件时,找不到该事件:
Ahoy::Event.where("properties LIKE '%\"article_id\": \"562\"%'").count
(207.5ms) SELECT COUNT(*) FROM "ahoy_events" WHERE (properties LIKE '%"article_id": "562"%')
=> 0
任何建议都非常感谢。