Rails 3: Quoting through an array of objects, ignoring the first object in the array?

In my opinion, I am trying to parse a table of objects, this is my code:

<div id='categories_show'>

  <table border="1">
    <tr>
      <th>Categories</th>
      <th>CBB's</th>
    </tr>
    <% for category in @critical_process.categories %>
        <tr>
          <td rowspan="<%= category.capability_building_blocks.size %>"><%= category.category_title %></td>
          <td><%= category.capability_building_blocks.first.cbb_title %></td>

        </tr>
        <% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>
        <tr>
          <td><%= cbb.cbb_title %></td>
        </tr>
        <% end %>
    <% end %>

  </table>
</div>

however, this causes an error: can't convert CapabilityBuildingBlock into Array

the relationship is correct, the error comes from the line where I try to subtract the first array object here: <% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>

is there any way i can iterate over an array ignoring the first object in the array?

thank

+5
source share
3 answers

Try using Array.drop - http://www.ruby-doc.org/core/classes/Array.html#M000294

<% category.capability_building_blocks.drop(1).each do |cbb| %>
  <tr>
    <td><%= cbb.cbb_title %></td>
  </tr>
<% end %>
+12
source

Also, this is more readable (and I'm sure it works 80%):

<%= category.capability_building_blocks[1..-1].each do |cbb| %>

, . -1 .

+4
<%= (category.capability_building_blocks - [category.capability_building_blocks.first]).each do |cbb| %>

Also...

stop_here = category.capability_building_blocks.length
category.capability_building_blocks[1..(stop_here)].each do |cbb|
+3
source

All Articles