# Writing an "upto" block in Rails

**URL:** <https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106>\
**Category:** Rails\
**Created:** [March 27, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106 "2008-03-27T20:28:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sara231](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sara231](https://www.ruby-forum.com/u/sara231)\
**Post date:** [March 27, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106/1 "2008-03-27T20:28:57Z")

</div>

I am trying to loop through records in a db and pull out a particular  
field. I have no problem with the code when I run it in the console;  
however when I try to transfer the code to my Rails environment it  
fails.

The code I’m using in my console is:

show = Schedule.find(:all)  
n = show.size - 1  
0.upto(n) { |i| puts show[i].venue }

The above code runs perfectly. To adapt this code for Rails, I am doing  
the following:

(IN THE CONTROLLER)  
class ScheduleEditController \< ApplicationController  
def index  
@show = Schedule.find(:all, :order =\> ‘date ASC’)  
end  
end

(IN THE VIEW)

\<%= render :partial =\> 'shows' %\>

(IN THE PARTIAL)  
\<% n = @show.size - 1 %\>

- \<%= 0.upto(n) { |i| puts @show[i].date } %\>

The result I get is this:

- 0

The result I am looking for is:

- 2
- 3
- 1

Yes, I am obviously a noob. Please help shed light on where I’ve gone  
wrong before I tear all my hair out.

---

<div class="post-metadata">

**Author:** ![sara231](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sara231](https://www.ruby-forum.com/u/sara231)\
**Post date:** [March 27, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106/2 "2008-03-27T20:48:35Z")

</div>

On 27 Mar 2008, at 19:28, Sara Me wrote:

> (IN THE PARTIAL)  
> \<% n = @show.size - 1 %\>
> 
> - \<%= 0.upto(n) { |i| puts @show[i].date } %\>

When you have a n erb output block (\<%= … %\>) what appears in the  
view is what the expression evaluates to. calls to puts inside there  
are immaterial n.upto evaluates to n, hence the result you’re getting  
You need something like

\<% 0.upto(n) do %\>

- \<%= @show[i].date %\> 
\<% end %\>

That’s not a very idiomatic way of doing it though. More idiomatic  
would be  
\<% @show.each do |show| %\>

- \<%= show.date %\> 
\<% end %\>

or using a partial (although it might be a bit overkill in this  
particular case)

Fred

---

<div class="post-metadata">

**Author:** ![sara231](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sara231](https://www.ruby-forum.com/u/sara231)\
**Post date:** [March 27, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106/3 "2008-03-27T20:51:45Z")

</div>

Sara Me wrote:

> (IN THE PARTIAL)  
> \<% n = @show.size - 1 %\>
> 
> - \<%= 0.upto(n) { |i| puts @show[i].date } %\>

The #puts method won’t output into the view…

In the view, put:

\<%= render :partial =\> ‘shows’, :collection =\> @show %\>

This will cause the partial to run for each item in the @show list.  
This saves you from iterating over them yourself.

Then, in the partial, put:

- \<% shows.date %\>

There will be a local variable of the same name as the partial (shows in  
this case) for each item in the collection.

For your iteration in your console session, you could more easily do:

show = Schedule.find(:all)  
show.each {|s| puts s.venue}

It is very rare in Ruby code to ever need to refer to indices at all.

---

<div class="post-metadata">

**Author:** ![sara231](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sara231](https://www.ruby-forum.com/u/sara231)\
**Post date:** [March 28, 2008, 12:20am UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106/4 "2008-03-28T00:20:30Z")

</div>

Jon G. wrote:

> > 
> 
> First change your @show to @shows (it has more than one, so for  
> readability, make it plural)  
> Second, change your partial to \_show, not \_shows.
> 
> Next  
> In your view, do this…
> 
> \<%= render :partial =\> 'show', :collection =\> @shows %\>
> 
> And then your partial can just be…
> 
> - \<%= show.date %\>
> 
> No need to to the upto loop. passing :collection to render will iterate  
> through items and re-render the partial for each.

YESSSS!!! Thank you so much. I understand it now.

---

<div class="post-metadata">

**Author:** ![sara231](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sara231](https://www.ruby-forum.com/u/sara231)\
**Post date:** [March 27, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/writing-an-upto-block-in-rails/133106/5 "2008-03-27T21:24:53Z")

</div>

Sara Me wrote:

> (IN THE VIEW)
> 
> Yes, I am obviously a noob. Please help shed light on where I've gone wrong before I tear all my hair out.

First change your @show to @shows (it has more than one, so for  
readability, make it plural)  
Second, change your partial to \_show, not \_shows.

Next  
In your view, do this…

\<%= render :partial =\> 'show', :collection =\> @shows %\>

And then your partial can just be…

- \<%= show.date %\>

No need to to the upto loop. passing :collection to render will iterate  
through items and re-render the partial for each.

–

[http://www.5valleys.com/](http://www.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)
