在带有数组rails的循环中执行if else

时间:2016-01-22 05:40:16

标签: ruby-on-rails arrays

目前我从数据库中获取大量记录并将这些记录包装在一个数组中,但我想在其中设置if条件

@reports = SnapshotReport.includes(camera: [:user]).where(report_date: date)
records = []
@reports.each do |report|
  next if report.camera.cloud_recording.blank?
  records[records.length] = [
    report.camera["name"],
    report.camera["exid"],
    report.camera.user["firstname"],
    report.camera.cloud_recording["storage_duration"],
    report.camera["is_online"],
    report["snapshot_count"],
    report.camera["id"],
    report.camera.user["id"],
    report.camera.user["lastname"],
  ]
end

现在正在做的是,使用next if跳过这些值

next if report.camera.cloud_recording.blank?

是null,但我想做这个事情数组制作部分但是就像那样 如果report.camera.cloud_recording为null,则会保存""空字符串,但如果存在report.camera.cloud_recording,则获取值report.camera.cloud_recording["storage_duration"]

1 个答案:

答案 0 :(得分:0)

使用

@reports = SnapshotReport.includes(camera: [:user]).where(report_date: date)
records = []
@reports.each do |report|
  records[records.length] = [
    report.camera["name"],
    report.camera["exid"],
    report.camera.user["firstname"],
    report.camera.cloud_recording || {"storage_duration" => ""} ["storage_duration"],
    report.camera["is_online"],
    report["snapshot_count"],
    report.camera["id"],
    report.camera.user["id"],
    report.camera.user["lastname"],
  ]
end