# Rails loop

**URL:** <https://www.ruby-forum.com/t/rails-loop/111274>\
**Category:** Rails\
**Created:** [August 4, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/rails-loop/111274 "2007-08-04T18:59:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rornb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rornb](https://www.ruby-forum.com/u/rornb)\
**Post date:** [August 4, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/rails-loop/111274/1 "2007-08-04T18:59:45Z")

</div>

Hi everyone

Can you please tell me where I can find a good tutorial on loops. By  
loop I mean something that is related with objects created from a model.  
I think is called an array loop. And something that combines from 2  
tables as well as setting condition which reads only some of the values  
of the table. For example on a shoping cart may have something which is  
paid and some which are pending.

Thanx

---

<div class="post-metadata">

**Author:** ![rornb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rornb](https://www.ruby-forum.com/u/rornb)\
**Post date:** [August 4, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/rails-loop/111274/2 "2007-08-04T20:51:02Z")

</div>

Dom T. wrote:

> Hi everyone
> 
> Can you please tell me where I can find a good tutorial on loops. By  
> loop I mean something that is related with objects created from a model.  
> I think is called an array loop. And something that combines from 2  
> tables as well as setting condition which reads only some of the values  
> of the table. For example on a shoping cart may have something which is  
> paid and some which are pending.
> 
> Thanx

Like loop through all the objects that the database brings back?  
Well if it is this, say you have a controller action that looks like  
this:

def cart  
@product\_orders = Order.find(:all, :conditions =\> ‘user\_id = 4’)  
@pending = Order.find(:all, :conditions =\> ‘user\_id = 4 and pending  
= 1’)  
end

Now in you view you would do something like:  
\<% foreach order in @product\_orders %\>  
  
Order #: \<%= order.id %\>  
\<% end %\>  
In this case I used a foreach loop.  
Alternatively you could do

\<% @product\_orders.each do |order| %\>  
Order #:\<%= order.id %\>  
\<% end %\>

I hope this helps.

---

<div class="post-metadata">

**Author:** ![rornb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rornb](https://www.ruby-forum.com/u/rornb)\
**Post date:** [August 4, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/rails-loop/111274/3 "2007-08-04T21:24:52Z")

</div>

You should definitely take some book in hands.  
Pick any Rails related or start from the  
[Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/docs),  
and try to pass all tutorials.  
All things will be mush clear.

---

<div class="post-metadata">

**Author:** ![rornb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rornb](https://www.ruby-forum.com/u/rornb)\
**Post date:** [August 4, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/rails-loop/111274/4 "2007-08-04T22:18:40Z")

</div>

Dom T. wrote:

> Hi Scott
> 
> Yes, that was a very useful example, thank you. Any more or pointers  
> were i can experiment ?

Definitely pick up a ruby book, or find tutorials online to understand  
the language (if you already know an OOP language it should look  
familar). Then pick up a rails book, Agile Web D. w/ Rails by  
Dave T. is really good, and there are other ones out there too,  
which probably teach the same stuff, it’s just this one is really in  
depth.

---

<div class="post-metadata">

**Author:** ![rornb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rornb](https://www.ruby-forum.com/u/rornb)\
**Post date:** [August 4, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/rails-loop/111274/5 "2007-08-04T21:24:14Z")

</div>

Hi Scott

Yes, that was a very useful example, thank you. Any more or pointers  
were i can experiment ?
