Home > Mobile >  ROR: Mapping array to get value
ROR: Mapping array to get value

Time:02-08

I need a small help on ROR.

I have a return from a query and when I do puts result.to_a, I have got:

{"clazz_id"=>1, "staff_id"=>1}
{"clazz_id"=>2, "staff_id"=>1}
{"clazz_id"=>3, "staff_id"=>1}

I have like to convert it to an array of [1, 2, 3] from clazz_id and how can I achieve that?

CodePudding user response:

if use Active record query can make this direct pluck

results.pluck(:clazz_id)

if you need deal with array of hash can use map or map! like

p results.to_a.map{|e| e["clazz_id"] } 

or can use collect

p results.to_a.collect{|e| e["clazz_id"] } 
  •  Tags:  
  • Related