简体   繁体   中英

How do you iterate over a range of numbers with each do with/ ERB Ruby on Rails 6?

I am attempting to iterate over a range of years for a method that passes on unique years to a where clause in ruby on rails. For some odd reason, it does not fire off when I attempt to pass those numbers down to the method chain. For starters, I am working on a reporting feature that tracks charts by years in between a range. For example: Search starts at 2020 and ends at 2024. All years from 2020 to 2024 will show a report for each year compiled together. Right now, I only get one year and I should be getting 2020, 2021 and 2024 on one response..How can I print out a range of years by integer and pass them through the each block to the corresponding method?

Example code below:

params {start_year: "2019", end_year: "2020"}

params[:start_year], params[:end_year]

<% [@start_year.to_i+1...@end_year.to_i].each do |facility_year| %>
   <% @facility.chart_records.where('extract(year from record_date) =?', facility_year).limit(11).each do |chart_record| %>
<tr>
<td><%= chart_record.record_date.strftime("%Y") %></td>
<td>Month: Jan</td>
<td><%= ChartRecord.where(facility_id: @facility.id).where('extract(month from record_date) =?', 1).where('extract(year from record_date) =?', facility_year).sum(:fit_jeans_count)</td>
</tr>
<% end %>

I just want to get one unique year from the start_year+1 count up until the end_year count and then display one record for each year in the range..

For ranges use parentheses (@start_year.to_i+1...@end_year.to_i) not brackets. With brackets, you create an array with only one item that contains a range. With parentheses, you'll get a range right away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM