test
test

Reputation: 2466

Ruby on Rails - make count with name group

I'm tying to show active user names using this code:

<%= User.count(:activeuser, :distinct => true, :group => 'name') %>

currently it showing this ugly result:

{"Webster"=>0, "Wilfrid"=>0, "Winifred"=>0, "Nicolas"=>1, "Cage"=>1}

how can i make this pretty? like showing only true users Nicolas and Cage

Upvotes: 0

Views: 37

Answers (1)

Jordan Running
Jordan Running

Reputation: 106027

If you want to filter a Hash to include only items that meet particular criteria, use Hash#select:

hsh = {"Webster"=>0, "Wilfrid"=>0, "Winifred"=>0, "Nicolas"=>1, "Cage"=>1}

hsh.select {|key, val| val > 0 }
# => {"Nicolas"=>1, "Cage"=>1}

...or, to exclude items, Hash#reject:

hsh.reject {|key, val| val.zero? }
# => {"Nicolas"=>1, "Cage"=>1}

Upvotes: 1

Related Questions