Ruby on rails 按列匹配的Rails zip数组

Ruby on rails 按列匹配的Rails zip数组,ruby-on-rails,Ruby On Rails,我有两个数组,我使用zip方法将它们连接在一个循环中。我遇到了一个问题,当zip方法中不存在记录时,它会抛出数据的顺序 例如: 我的看法 但由于@rain-zip方法中不存在site 2,因此它将第二行数据放在末尾。我有两个变量,因为我每天早上8点计算总降雨量 控制器 因此,站点2在所选的任何一天的上午8点都没有降雨,因此@rain变量中不存在降雨。我试图通过site_no的条件匹配,但仍然没有成功 模式 正确的方法是建立一个两表系统并分组、连接和选择聚合 # rails g model sit

我有两个数组,我使用zip方法将它们连接在一个循环中。我遇到了一个问题,当zip方法中不存在记录时,它会抛出数据的顺序

例如:

我的看法

但由于@rain-zip方法中不存在site 2,因此它将第二行数据放在末尾。我有两个变量,因为我每天早上8点计算总降雨量

控制器

因此,站点2在所选的任何一天的上午8点都没有降雨,因此@rain变量中不存在降雨。我试图通过site_no的条件匹配,但仍然没有成功

模式


正确的方法是建立一个两表系统并分组、连接和选择聚合

# rails g model site name lat:decimal lon:decimal
class Site < ApplicationRecord
  # This is a truely ridiculous name
  # @see https://en.wikipedia.org/wiki/Weathers_(band)
  has_many :weathers 

  def self.with_aggregates(from: 7.days.ago, to: Time.current)
    w = Weather.arel_table
    left_joins(:weathers)
      .select(
        :id,
        w[:rain].sum.as('total_rain'),
        w[:temperature].average.as('average_temperature'),
        w[:temperature].maximum.as('high_temperature'),
        w[:temperature].minimim.as('low_temperature')
      )
      .group(:id)
      .where(weathers: { timestamp: from..to })
  end
end
这将使您可以简单地显示它,而无需压缩任何内容,因为您实际上做得很好,并让数据库进行数字处理

<% Site.with_aggregates.each do |site| %>
 <%= site.id %> | <%= site.average_temperature %> | <%= site.total_rain %>
<% end %>

我想你错过了一个网站模型来整合所有这些。谢谢@Schwern。我已经添加了模式。没有站点模型,站点是天气表的一部分。我建议使用站点模型。它有生成7天平均值的方法。将站点传递给您的视图。或者传递一个散列,其中键是站点号{1:{temp:20,rain:10},2:{temp:22,rain:0},3:{temp:18:,rain:3}
@temperature = Weather.select('DISTINCT ON(site_no) site_no, temperature, lat, long, timestamp').group(:site_no, :temperature, :lat, :long, : timestamp).order(: site_no).where('timestamp > ?', 7.days.ago)
@rain = Weather.select('site_no, SUM(rain) as total_rain').where('timestamp > ?', 7.days.ago).where('timestamp::time = ?', "08:00").group(:site_no)
create_table "weathers", force: :cascade do |t|
 t.integer "site_no"
 t.decimal "temperature"
 t.decimal "rain"
 t.datetime "timestamp"
 t.decimal "lat"
 t.decimal "long"
end
# rails g model site name lat:decimal lon:decimal
class Site < ApplicationRecord
  # This is a truely ridiculous name
  # @see https://en.wikipedia.org/wiki/Weathers_(band)
  has_many :weathers 

  def self.with_aggregates(from: 7.days.ago, to: Time.current)
    w = Weather.arel_table
    left_joins(:weathers)
      .select(
        :id,
        w[:rain].sum.as('total_rain'),
        w[:temperature].average.as('average_temperature'),
        w[:temperature].maximum.as('high_temperature'),
        w[:temperature].minimim.as('low_temperature')
      )
      .group(:id)
      .where(weathers: { timestamp: from..to })
  end
end
# for the love of god use the correct terms 
# Observation or Forecast instead of abusing the english language
class Weather < ApplicationRecord
  belongs_to :site # Use `site_id` instead of `site_no`.
end
<% Site.with_aggregates.each do |site| %>
 <%= site.id %> | <%= site.average_temperature %> | <%= site.total_rain %>
<% end %>