# Is there a better way to do this? - newbie question

**URL:** <https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417>\
**Category:** Rails\
**Created:** [March 28, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417 "2007-03-28T16:52:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 28, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417/1 "2007-03-28T16:52:54Z")

</div>

I’m deploying and running into problems that I didn’t have in the  
development environment…

In development, I have a section of code that says something like:

\<% for charity in @charities %\>  
\<%= blah blah blah %\>  
\<% end %\>

and it works fine…

when I go to deployment, it throws an error saying it doesn’t recognize  
this class variable (because there weren’t any charities entered in the  
db).

When I put an ‘if’ statement in front of the ‘for’ loop, it works:

\<% if @charities %\>  
\<% for charity in @charities %\>  
\<%= blah blah blah %\>  
\<% end %\>

I kinda have 2 questions:

1. Why does not having the ‘if’ statement work in development, but  
not in production?
2. Is there a cleaner way to write this code?

Thanks a HEAP 🙂

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 28, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417/2 "2007-03-28T17:06:13Z")

</div>

Dustin,

You are probably not initializing @charities in the controller if  
there are none. I guess you could do this (see below) although I  
would probably just check your controller code and always init  
@charities…

\<% for charity in (@charities || []) %\>  
\<%= blah blah blah %\>  
\<% end %\>

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 28, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417/3 "2007-03-28T17:11:17Z")

</div>

> I kinda have 2 questions:
> 
> 1. Why does not having the ‘if’ statement work in development, but  
> not in production?

It works in production because you said there are rows in the production  
database that get populated into @charities. So @charities contains  
something.

> 1. Is there a cleaner way to write this code?

How are you populating @charities? Even in development with no records  
@charities should get set to [] in which case that for loop should  
work…

-philip

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 28, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-newbie-question/90417/4 "2007-03-28T19:06:38Z")

</div>

Philip H. wrote:

> > 
> 
> How are you populating @charities? Even in development with no records  
> @charities should get set to [] in which case that for loop should  
> work…
> 
> -philip

Thanks gang - I’m running edge rails and screwed that up… we didn’t  
have the vendor/rails directory deploying - so I think it was running on  
an older version of rails that wasn’t as “smart”…
