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

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 :slight_smile:

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

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

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”…