# Understanding Find Requests & Loops Correctly

**URL:** <https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178>\
**Category:** Rails\
**Created:** [June 18, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178 "2007-06-18T20:31:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [June 18, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178/1 "2007-06-18T20:31:03Z")

</div>

Good afternoon,

Everyone here has been incredibly helpful in their past support of my  
issues, so I’m hoping that I can ask just a few more as I move along in  
learning RoR.

I’m generating two grids in a particular view. The first grid displays  
the top five selling products (as defined by the field, position) and  
then the last grid displaying the remaining products.

The existing find query is as follows:

```
@product_grid = Product.find(:all, :order => 'position', :conditions

```

=\> {:store\_id =\> current\_store.id})

After retrieving all of the information for the relevant products, the  
array is passed over to a view that displays it as follows:

\<% @product\_grid.in\_groups\_of(5, false) do |row\_products| %\>

\<% for product in row\_products %\>  
\<%= product.name%\>

\<% end %\>  
\<% end %\>

How would one go about limiting this loop to only display the first 5  
products that were retrieved based on position? Additionally, how would  
one use the same grid format to display every product that has a  
position greater than 5?

---

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [June 18, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178/2 "2007-06-18T23:42:30Z")

</div>

Neil H. wrote:

> The existing find query is as follows:  
> \<% for product in row\_products %\>  
> \<%= product.name%\>
> 
> \<% end %\>  
> \<% end %\>
> 
> How would one go about limiting this loop to only display the first 5  
> products that were retrieved based on position? Additionally, how would  
> one use the same grid format to display every product that has a  
> position greater than 5?

How about:

first\_five = @product\_grid[0…4]  
everything\_else = @product\_grid[5…@product\_grid.length]

–  
Cheers,

- Jacob

---

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [June 19, 2007, 12:09am UTC](https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178/3 "2007-06-19T00:09:02Z")

</div>

Would that approach toss an error if the total array size was less than  
5 when declaring everything\_else?

> How about:
> 
> first\_five = @product\_grid[0…4]  
> everything\_else = @product\_grid[5…@product\_grid.length]
> 
> –  
> Cheers,
> 
> - Jacob

---

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [June 19, 2007, 9:58am UTC](https://www.ruby-forum.com/t/understanding-find-requests-loops-correctly/107178/4 "2007-06-19T09:58:50Z")

</div>

Neil H. wrote:

> Would that approach toss an error if the total array size was less than  
> 5 when declaring everything\_else?

No, in that case the variable will simply be nil.

–  
Cheers,

- Jacob A.
