Monday, June 6, 2011

How to iterate over table columns and get values

In one of my application, there is a table, Product with more than 100 columns. I wanted to iterate over all the columns and display their values on product show page.

This is how I did it:
<table border="1">
    <% for column in Product.content_columns %>
          <th><%= column.human_name %></th> 
          <td><%= h @product.send( %> <td>
    <% end %>

column.human_name - Returns the human name of the column name. If your column is "product_owner", it will return "Product owner". will return the name of the column (product_owner)
@product.send( will return the value of the column.

No comments:

Post a Comment