"You have a nil object" problem driving me crazy!

Hi!

I've got the following problem:

FYI: - @dilemmas is populated in the controller via: @dilemmas = Dilemma.find(:all) - the Dilemma model has_many :dilemma_sides - the DilemmaSide model belongs_to :dilemma - the DilemmaSide model has_many :side_images - the SideImage model belongs_to :dilemma_side - side_images table has following attributes # Table name: side_images

Here's the code from my view.

[code=]<% @dilemmas.each do |dilemma| %>    <% dilemma.dilemma_sides.each do |dilemma_side| %>

     <% side_image = dilemma_side.side_images.first %> <%= side_image.filename.to_s %>

   <%end%> <% end %>[/code] When I execute the code, I get the following error:

You have a nil object when you didn't expect it! The error occurred while evaluating nil.filename

The obvious answer would be that you have a dilemma side with no side images.

Fred

Hi,

I've made a mistake in my description above. Here's the corrected version:

If I replace the

[code=]<%= side_image.filename.to_s %>[/code]

with

[code=]<%= debug(side_image.filename) %>[/code]

I'm desperate! Thank you for your help!

Kind regards, John

Frederick Cheung wrote:

The obvious answer would be that you have a dilemma side with no side images.

Fred

Hi Fred,

Thank you, but as you can see above, the output of the debug command above, side_image does contain a Side Image:

--- !ruby/object:SideImage attributes:   content_type: image/jpeg   size: "44882"   thumbnail:   updated_at: 2008-11-28 19:06:35   id: "1"   dilemma_side_id: "1"   height: "256"   filename: IMG_0026.JPG   parent_id:   width: "341"   created_at: 2008-11-28 19:06:35 attributes_cache: {}

I'm just not able to access the attributes!

Cheers, John

Frederick Cheung wrote:

The obvious answer would be that you have a dilemma side with no side images.

Fred

Hi Fred,

Thank you, but as you can see above, the output of the debug command above, side_image does contain a Side Image:

--- !ruby/object:SideImage attributes: content_type: image/jpeg size: "44882" thumbnail: updated_at: 2008-11-28 19:06:35 id: "1" dilemma_side_id: "1" height: "256" filename: IMG_0026.JPG parent_id: width: "341" created_at: 2008-11-28 19:06:35 attributes_cache: {}

I'm just not able to access the attributes!

Cheers, John --
Posted via http://www.ruby-forum.com/.

>

Frederick Cheung wrote:

The obvious answer would be that you have a dilemma side with no side images.

Fred

Hi Fred,

Thank you, but as you can see above, the output of the debug command above, side_image does contain a Side Image:

Are you sure that's not displaying the side image from a previous
iteration through the loop ?

How about you change your code to

<% @dilemmas.each do |dilemma| %>     <% dilemma.dilemma_sides.each do |dilemma_side| %>

      <% side_image = dilemma_side.side_images.first %>       <% if !side_image %>         Side <%= dilemma_side.id %> has no images       <% end %>     <%end%> <% end %>

and see what it outputs.

Fred

Damn, again made a mistake in my description:

[code=]<%= side_image.filename.to_s %>[/code]

with

[code=]<%= debug(side_image) %>[/code]

That's it guys, that's the correct version. Sorry for the confusion!

How about you change your code to

<% @dilemmas.each do |dilemma| %>     <% dilemma.dilemma_sides.each do |dilemma_side| %>

      <% side_image = dilemma_side.side_images.first %>       <% if !side_image %>         Side <%= dilemma_side.id %> has no images       <% end %>     <%end%> <% end %>

and see what it outputs.

Fred

Hey,

I've tried it, and as expected, it does not go into the loop. I am sure that it is finding the Side Image belonging to the Dilemma Side. For some strange reason, when I loop through the array which contains the Side Images:

<% dilemma_side.side_images.each do |side_image|%>   <%= side_image.filename.to_s %>         <%end>

Then Rails allows me to access the attribute directly. I'm certainly doing something wrong with instance variables assignments/declarations.

Hi --

Is the code you're showing us cut-and-pasted from your views? It looks like it might be a typo (like @side_image for side_image), so to rule that out I wanted to be sure this is the actual template code.

Hi David,

Yes, it is an actual copy an paste.

Thanks! John

Could you maybe copy and paste it into pastie so that it is easier for everyone to look at?