Ruby on rails rails组匹配数组中的日期
我有一个来自关联的数组。我希望生成一个表,以表系列的形式显示数据,将每行数据与日期匹配 可以有许多处理,范围从1到6,但每次处理总是有3个条目 控制器Ruby on rails rails组匹配数组中的日期,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我有一个来自关联的数组。我希望生成一个表,以表系列的形式显示数据,将每行数据与日期匹配 可以有许多处理,范围从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 :a
@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>
试验、治疗选择、检查的输出
#<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 id: 121, repetitions: 3, area: 18.0, comment: nil, no_of_treatment: 4, created_at: 1531358319000, update_at: 1531358319000>
#<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
#你能发布@trial.treatment_selections.inspect的输出吗
和@trial.inspect
?你能分享你问题中提到的所有模型的模式吗?向post添加了模式。添加了@trial.treatment_selections.inspect的输出。还有@trial.inspect
和@trial.methods.inspect如果你可以。无论如何,请检查添加distinct
是否解决了您的问题:@trial.methods.distinct.order(:treatment\u selection\u id)…
#<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