/ / Запитване за Ahoy Gem Събития - руби-на-релси, рубин, хеш, ахой

Запитване за Ahoy Gem Събития - ruby-on-rail, ruby, hash, ahoy

Аз съм с Ahoy скъпоценен камък в Rails 4.2 ап. Ahoy Events съхранява събития с няколко текстови атрибута и a properties атрибут, който съдържа данни, които искам да заявя. Например тук е последното събитие, което се връщам чрез конзолата на релсите

>> 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"] => "Преглед") 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блок в 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:in 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:in where" 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: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: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:in require" 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:inблок в товара " от /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:inнатоварване " от /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require" from /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inизисква " `` `

Търсих онлайн за запитване за хеш атрибути без успех. Всяко ръководство е много ценено.

Отговори:

2 за отговор № 1

Ahoy :: Събитието всъщност не връща нищо полезно. За да получите всички записи в обект връщане на масив, трябва да използвате Ahoy::Event.all.

Така че това, което всъщност трябва да направите, е нещо, подобно на това:

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

Времето на работа няма да бъде толкова хубаво, но мисля, че това е, от което се нуждаете.


1 за отговор № 2

употреба Ahoy::Event.where("properties ->> "text" = "View"") По-бързо използване на валиден JSONB заявка синтаксис.