У меня есть массив, который вытекает из ассоциаций. Я пытаюсь создать таблицу, отображающую данные в серии таблиц, сопоставляя каждую строку данных с датой.
Может быть много процедур от 1 до 6, но для каждой процедуры всегда будет 3 записи.
Контроллер
@trial = Trial.find(params[:trial_id])
@max_length = [@trial.treatment_selections].map(&:size).max
Модель
has_many :treatment_selections, primary_key: 'trial_id'
has_many :assessments, primary_key: 'trial_id'
has_many :methods, through: :assessments
Структура таблицы
<table class="table table-bordered">
<th>Date</th>
<th>Degree</th>
<% @max_length.times do |data| %>
<th><%= @trial.treatment_selections[data].try(:treatment).try(:name) %></th>
<% end %>
<% @trial.methods.order(:treatment_selection_id).order("assessment_date ASC").group(:assessment_id).each do |e| %>
<tr>
<td><%= Time.at(e.try(:assessment).try(:assessment_date)/1000).strftime("%d/%m/%Y") rescue 0 %></td>
<td><%= e.try(:assessment).try(:degrees) rescue 0 %></td>
<% @trial.methods.order(:treatment_selection_id).order("assessment_date ASC").group(:treatment_selection_id).each do |f| %>
<td><%= f.try(:total).round(1) rescue 0 %></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
Приведенный выше код отображает следующее:

Кажется, повторяются первые данные оценки для каждого лечения в каждом ряду. Когда на самом деле должно быть больше данных, например:

Schemas
create_table "methods", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "treatment_selection_id"
t.integer "assessment_id"
t.float "total"
end
create_table "treatment_selections", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "trial_id", limit: 36
t.integer "treatment_id", limit: 36
t.integer "quantity"
t.text "bar_code"
end
create_table "assessments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "trial_id", limit: 36
t.bigint "assessment_date"
t.integer "degrees"
t.integer "flowering"
t.integer "sowing"
t.integer "hot_days"
end
Вывод @trial.treatment_selections.inspect
#<ActiveRecord::Associations::CollectionProxy [#<ActiveRecord::Associations::CollectionProxy [#<Treatment_selection id: 372, trial_id: 121, treatment_id: 2, quantity: nil, ginBarCode: nil>, #<ActiveRecord::Associations::CollectionProxy [#<Treatment_selection id: 373, trial_id: 121, treatment_id: 1, quantity: nil, ginBarCode: nil>, #<ActiveRecord::Associations::CollectionProxy [#<Treatment_selection id: 374, trial_id: 121, treatment_id: 3, quantity: nil, ginBarCode: nil>, #<ActiveRecord::Associations::CollectionProxy [#<Treatment_selection id: 375, trial_id: 121, treatment_id: 4, quantity: nil, ginBarCode: nil>]>
Вывод @trial.inspect
#<Trial id: 121, repetitions: 3, area: 18.0, comment: nil, no_of_treatment: 4, created_at: 1531358319000, update_at: 1531358319000>
Вывод @trial.methods.inspect
#<ActiveRecord::Associations::CollectionProxy [#<Method id: 1114, treatment_selection_id: 372, assessment_id: 98, total: 0.1>, #<Method id: 1115, treatment_selection_id: 372, assessment_id: 98, total: 2.1> ... etc