Rails组匹配数组中的日期

时间:2018-08-28 03:43:08

标签: 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 :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>

上面的代码显示以下内容:

enter image description here

似乎在每一行中为每种处理重复了第一批评估数据。实际上,应该显示更多数据,例如:

enter image description here

模式

 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

0 个答案:

没有答案