Rails loop

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

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.

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.,
and try to pass all tutorials.
All things will be mush clear.

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.

Hi Scott

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