Reputation: 28783
I have an Array like so:
search_results = [{policy: {isActive: true}}]
And I want to get the count of search_result
s where isActive
is true. In this case, the Array only contains 1 item whose policy
isActive
object is true
.
I tried with:
search_results.group_by { |x| x.policy.isActive }.count
However I always get 1
back. Regardless if isActive
is true or false.
Upvotes: 0
Views: 113
Reputation: 52357
You access hash key-value pair with []
, not .
.
search_results.count { |hash| hash[:policy][:isActive] }
If you're using newer Ruby version, you can make use of Hash#dig
(Ruby 2.3+) and Enumerable#sum
(Ruby 2.4+):
search_results.sum { |hash| hash.dig(:policy, :isActive) }
Upvotes: 1