目前我从数据库中获取大量记录并将这些记录包装在一个数组中,但我想在其中设置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"]
答案 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