if
@users = User.all.group_by(&:iso).map{|k,v| [k, v.count]}
returns
[["GB", 1], ["GI", 3], ["BD", 1]]
Why can't I do this
@users = User.all.group_by(&:iso).map{|k,v| [k, v.count, k.downcase]}
to get
[["GB", 1, "gb"], ["GI", 3, "gi"], ["BD", 1, "bd"]]
I am getting the error
undefined method `downcase' for nil:NilClass
How do I get this.
If I do
@user_json = User.all.group_by(&:iso).map{|k,v| [k, v.count, k.inspect]}
I get
[["GB", 1, "\"GB\""], ["GI", 3, "\"GI\""], ["BD", 1, "\"BD\""]
and then
@user_json = User.all.group_by(&:iso).map{|k,v| [k, v.count, k.inspect.downcase]}
[["GB", 1, "\"gb\""], ["GI", 3, "\"gi\""], ["BD", 1, "\"bd\""]
V. confused
FOR MORE DETAILS - THIS WORKS (but if it is wrong and can't explain why)
I am trying to build a JSON array with the structure:
[{"code":"GB","value":1,"flag":"gb"},{"code":"GI","value":3,"flag":"gi"},{"code":"BD","value":1,"flag":"bd"}]
My controller code
def global_chart
@user_json = User.all.group_by(&:iso).map{|k,v| [k, v.count, k.to_s.downcase]}.map {|c, v | ["code" => c, "value" => v, "flag" => c.to_s.downcase]}.flatten.to_json
render 'users/charts/global'
end
This works ( the @user_json variable has the right structure) however I can't explain the behaviour above....
User.all.group_by(&:iso).map{|k,v| [k, v.count, k.try(:downcase)]}k.to_s.downcase?