0

I am using nested map on has_many association in following method

@trial.treatment_selections
.map { |ts| ts.establishment_methods.map { |em| em.final_establishment.to_f }}
# => [[10.2, 10.1, 10.1], [11.4, 11.4, 10.9]]

Here treatment_selections has_many establishment_methods.

I'm not sure how to get following array:

[10.2, 10.1, 10.1, 11.4, 11.4, 10.9]
Pramod Shinde
1,9021 gold badge15 silver badges29 bronze badges
asked Nov 27, 2018 at 2:38

2 Answers 2

2

Try flat_map:

<%= @trial.treatment_selections.flat_map { |ts| ts.establishment_methods.map { |em| em.final_establishment.to_f }} %>
answered Nov 27, 2018 at 2:40
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on. Cheers @emaillenin.
0

You can also use flatten method of an Array

@trial.treatment_selections
 .map { |ts| ts.establishment_methods.map { |em| em.final_establishment.to_f }}
 .flatten
#=> [10.2, 10.1, 10.1, 11.4, 11.4, 10.9]
answered Nov 27, 2018 at 8:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.