sqlruby-on-railsrubypostgresqlahoy

Ahoy events queries in rails


I started code rails recently and I need to figure out this problem.

%td
  =  Ahoy::Event.where(name: "viewed_brochure").where_properties(brochure_id: brochure.id).select(:session_id).uniq

This should find number of distinct values of session_id in properties field but it doesn't work.

This line returns these and similar errors

#<Ahoy::Event::ActiveRecord_Relation:0x007f52d9325130>
#<Ahoy::Event::ActiveRecord_Relation:0x007f52d9466508>

%td
  =  Ahoy::Event.select { |event| event.properties['session_id'] }.uniq.count

I tried this line but this time returns number of rows in table


Solution

  • You can try to group by jsonb key, in your case properties['session_id']

    Ahoy::Event.group("properties -> 'session_id'").count
    

    This will return the counts on which you can apply .size or you could do distinct.