1

Rails 4.2 アプリで Ahoy gem を使用しています。propertiesAhoy Events は、多数のテキスト属性とクエリしたいデータを含む属性を持つイベントを保存します。たとえば、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: 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:in/Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0 からの expand_hash_conditions_for_aggregates の未定義のメソッド ブロック/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:inwhere' from (irb):13 from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:in start' from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:instart' from /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:require<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: 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:in/Users/dariusgoore/development/blackops/.bundle/gems/ruby の「load_dependency」内/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' from /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' from /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' from /Users/dariusgoore/.rubies/ruby-2.2. 1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54: require' from /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' ```

ハッシュ属性のクエリをオンラインで調べましたが、成功しませんでした。どんなガイダンスも大歓迎です。

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'")有効な JSONB クエリ構文を使用するには、Way の高速化を使用してください。

于 2017-05-18T22:10:12.663 に答える