1

我在 Rails 4.2 应用程序中使用 Ahoy gem。Ahoy Events 存储具有许多文本属性和一个properties包含我要查询的数据的属性的事件。例如,这是我通过 rails 控制台返回的最后一个事件

>> Ahoy::Event.last Ahoy::Event Load (3.8ms) SELECT "ahoy_events".* FROM "ahoy_events" ORDER BY "ahoy_events"."id" DESC LIMIT 1 => #<Ahoy::Event id: "fffff17f-44e6-41d7-9c32-fa895338f54e", visit_id: "6b05b1ca-3863-46da-9c48-a3ab5fa4a9ee", user_id: 1, name: "$click", properties: {"tag"=>"a", "page"=>"/conversations/2", "text"=>"View", "href"=>"/conversations/2"}, time: "2015-08-23 13:25:51">

我将如何检索 properties['text'] = 'View' 的所有事件

我努力了Ahoy::Event.where(:properties['text'] => 'View').count

返回:```

Ahoy::Event.where(:properties['text'] => 'View').count NoMethodError: undefined method to_sym' for nil:NilClass from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1/lib/active_record/sanitization.rb:59:inblock in expand_hash_conditions_for_aggregates'来自/Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0 /gems/activerecord-4.2.5.1/lib/active_record/sanitization.rb:58:in each' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1/lib/active_record/sanitization.rb:58:inexpand_hash_conditions_for_aggregates' 来自 /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1 /lib/active_record/relation/query_methods.rb:957:build_where' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1/lib/active_record/relation/query_methods.rb:584:in在哪里!来自/Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1/lib/active_record/relation/query_methods.rb:574:inwhere' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:10:in哪里'来自(irb):13来自/Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:instart' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in从 /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in console' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:inrun_command 开始' 来自 /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in<top (required)>' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in来自 /Users/dariusgoore/development/ blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in block in require' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:inload_dependency' 来自 /Users/dariusgoore/development/blackops/.bundle/gems/ruby /2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:inrequire' from /Users/dariusgoore/development/blackops/bin/rails:8:in'来自 /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in load' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:inblock in load' 来自 /Users/dariusgoore /development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in load_dependency' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:inload' 来自 /Users/dariusgoore/.rubies/ruby-2.2。 1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in需要'```

我在网上查找了查询哈希属性但没有成功。非常感谢任何指导。

4

2 回答 2

2

Ahoy::Event 实际上并没有返回任何有用的东西。要获取数组返回对象中的所有记录,您需要使用Ahoy::Event.all.

所以你真正需要做的事情是这样的:

Ahoy::Event.all.select { |event| event.properties['text'] == "View" }.size

运行时间不会那么好,但我认为这是你需要的。

于 2016-03-02T17:21:04.567 回答
1

使用Ahoy::Event.where("properties ->> 'text' = 'View'")Way 更快地使用有效的 JSONB 查询语法。

于 2017-05-18T22:10:12.663 回答