jjabrams jjabrams - 7 months ago 10
Ruby Question

How to iterate after some data?

I have a product model with has_many images model

so on the first loop i just get the first 4 images

<% @product.images.first(4).each do |i| %>
<li>
<a href="<%= i.photo.url.to_s %>">
<%= image_tag(i.photo.url(:small).to_s, :class => 'thumbnail circle', :'data-zoom-href' => i.photo.url(:big).to_s) %>
</a>
</li>
<% end %>


how to loop the rest of images after the first 4 images?

i've tried this: without sucess!

<ul>


<% @product.images.last.each do |i| %>
<li>
<a href="<%= i.photo.url.to_s %>">
<%= image_tag(i.photo.url(:small).to_s, :class => 'thumbnail circle', :'data-zoom-href' => i.photo.url(:big).to_s) %>
</a>
</li>
<% end %>


</ul>

Answer

Use offset:

@product.images.offset(4).each { }

offset(4) means all records after first 4.

Comments