我在以下方法中对has_many关联使用嵌套映射
@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]]
这里treatment_selections
has_many establishment_methods
。
我不确定如何获取以下数组:
[10.2, 10.1, 10.1, 11.4, 11.4, 10.9]
答案 0 :(得分:2)
尝试flat_map:
<%= @trial.treatment_selections.flat_map { |ts| ts.establishment_methods.map { |em| em.final_establishment.to_f }} %>
答案 1 :(得分:0)
您还可以使用数组的flatten方法
@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]