rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

jpw

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.

Alaa Othman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

How display 3 words from difference table columns to a single listview column using vb.net?

From Dev

How to display all data in 3 columns from a table where the 4th column contains the same ID as another table?

From Dev

Rails 4 AR `from` different than rails 3?

From Dev

Lost automatic ActiveRecord caching by migrating from Rails 3 to Rails 4

From Dev

Lost automatic ActiveRecord caching by migrating from Rails 3 to Rails 4

From Dev

UPDATE 2 columns of a table of values from a table

From Dev

SUMmarize 3 columns from 2 different table in SQL

From Dev

How To Transpose Every 3 values From One row To Multiple columns?

From Dev

how to replace content of a column with values from another 3 columns?

From Dev

CakePHP 3: How to display sum of values in column from finder results

From Dev

how to show 2 value and one values count from 3 table using mysql

From Dev

How to make 3 columns table with Twig in Symfony2

From Dev

How to Remove SQLite3 from Rails but keep using ActiveRecord/Mongo?

From Dev

How to Remove SQLite3 from Rails but keep using ActiveRecord/Mongo?

From Dev

Finding the values from same table that have value 1 and 2 but not 3

From Dev

Is it possible to call the columns of the table1 in table3, going from 2nd table

From Dev

Selecting everything from table 1 with average AND distinct values from table 2 and table 3

From Dev

Join table 1 with 2. If no values found in table 2 fetch from table 3

From Dev

How to quickly SELECT 3 random records from a 30k MySQL table with a where filter by a single query?

From Dev

How to quickly SELECT 3 random records from a 30k MySQL table with a where filter by a single query?

From Dev

Get the values from 2nd and 3rd table for the matched values in 1st table

From Dev

Select distinct values from 3 columns into 1

From Dev

Max of values from 3 columns in a dataframe?

From Dev

Select distinct values from 3 columns into 1

From Dev

Extract values from pattern string, into 3 columns?

From Dev

ActiveRecord - Display columns from multiple associations

From Dev

ActiveRecord - Display columns from multiple associations

Related Related

  1. 1

    rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

  2. 2

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  3. 3

    How display 3 words from difference table columns to a single listview column using vb.net?

  4. 4

    How to display all data in 3 columns from a table where the 4th column contains the same ID as another table?

  5. 5

    Rails 4 AR `from` different than rails 3?

  6. 6

    Lost automatic ActiveRecord caching by migrating from Rails 3 to Rails 4

  7. 7

    Lost automatic ActiveRecord caching by migrating from Rails 3 to Rails 4

  8. 8

    UPDATE 2 columns of a table of values from a table

  9. 9

    SUMmarize 3 columns from 2 different table in SQL

  10. 10

    How To Transpose Every 3 values From One row To Multiple columns?

  11. 11

    how to replace content of a column with values from another 3 columns?

  12. 12

    CakePHP 3: How to display sum of values in column from finder results

  13. 13

    how to show 2 value and one values count from 3 table using mysql

  14. 14

    How to make 3 columns table with Twig in Symfony2

  15. 15

    How to Remove SQLite3 from Rails but keep using ActiveRecord/Mongo?

  16. 16

    How to Remove SQLite3 from Rails but keep using ActiveRecord/Mongo?

  17. 17

    Finding the values from same table that have value 1 and 2 but not 3

  18. 18

    Is it possible to call the columns of the table1 in table3, going from 2nd table

  19. 19

    Selecting everything from table 1 with average AND distinct values from table 2 and table 3

  20. 20

    Join table 1 with 2. If no values found in table 2 fetch from table 3

  21. 21

    How to quickly SELECT 3 random records from a 30k MySQL table with a where filter by a single query?

  22. 22

    How to quickly SELECT 3 random records from a 30k MySQL table with a where filter by a single query?

  23. 23

    Get the values from 2nd and 3rd table for the matched values in 1st table

  24. 24

    Select distinct values from 3 columns into 1

  25. 25

    Max of values from 3 columns in a dataframe?

  26. 26

    Select distinct values from 3 columns into 1

  27. 27

    Extract values from pattern string, into 3 columns?

  28. 28

    ActiveRecord - Display columns from multiple associations

  29. 29

    ActiveRecord - Display columns from multiple associations

HotTag

Archive