Suppose the query customer_list = Customers.where ("state = ?", 'CA').order('first_name ASC')
returns 30 customers, and the field .first_name has their first name.
I want to display their names in an html table with 3 more or less equal columns, eg, I want to fill the table as I go, wrapping to a new row each 3rd value.
Aaron Amy Andy
Arny Beth Bill
Bob Carl Charlie
etc etc
What is the 'ruby way' to flow the records into such a table's cells?
I could use .find_in_batches to grab 3 at a time, then iterate through the three for each column, but I suspect that's not the elegant ruby way.
The each_slice helper method accepts a number n and then breaks the array into chunks of n, in this case 3.
<% @customer_list.each_slice(3) do |slice| -%>
<tr>
<% slice.each do |customer| -%>
<td><%= customer.first_name %></td>
<% end -%>
<tr>
<% end -%>
See the documentations also check out this question.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments