1

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?

3
  • 1
    result.to_a.map{|e| e["clazz_id"] } Commented Feb 8, 2022 at 0:39
  • 1
    How did you get this result and how do you intend to use it? Generally speaking this would not be the outcome of a query so a better understanding might result in far better answers. Commented Feb 8, 2022 at 1:02
  • @engineersmnky, for now I will just use the the answer provided below. I am very new to ruby. Commented Feb 8, 2022 at 2:30

1 Answer 1

1

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"] } 
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.