I am using the Ahoy gem to record user actions in a Rails 4 app. Events are being recorded and stored in the Ahoy::Event table. Each event has a properties
hash attribute stored as text. In this case, am trying to return the number of article views for a specific article.
I am following the suggested syntax in the gem docs here: https://github.com/ankane/ahoy#querying-properties, which gives an example of Ahoy::Event.where("properties LIKE '%\"store_id\": \"1\"%'").count
for querying a properties text hash, but I have been unable to return accurate results.
Here is the controller event I am tracking to test this:
def show
@article = Article.friendly.find(params[:id])
...
ahoy.track "Viewed article", article_id: @article.id
...
end
In console, as expected I can see one event is returned when I search for articles with the name: "Viewed Article":
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">]>
But when I attempt to find the same event by querying for events with the corresponding article_id, the event is not found:
Ahoy::Event.where("properties LIKE '%\"article_id\": \"562\"%'").count
(207.5ms) SELECT COUNT(*) FROM "ahoy_events" WHERE (properties LIKE '%"article_id": "562"%')
=> 0
Any suggestions are much appreciated.