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 # # id :integer(4) not null, primary key # dilemma_side_id :integer(4) not null # parent_id :integer(4) # size :integer(4) # width :integer(4) # height :integer(4) # content_type :string(255) # filename :string(255) # thumbnail :string(255) # created_at :datetime # updated_at :datetime # 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 If I replace the [code=]<%= side_image.filename.to_s %>[/code] with [code=]<%= side_image.filename.to_s %>[/code] I can confirm that the side_image has is correctly pointing to a SideImage object: --- !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: {} If on the other hand, I change the code in the following way: [code=]<% @dilemmas.each do |dilemma| %> <% dilemma.dilemma_sides.each do |dilemma_side| %> <% dilemma_side.side_images.each do |side_image|%> <%= side_image.filename.to_s %> <%end> <%end%> <% end %>[/code] Then it works! But the problem with this solution is that I am looping through all side_images associated with the dilemma_side. I only want to display the first image. I have no idea why I am getting this nil object error, and I''ve tried everything I could think of. I am relatively new to Ruby on Rails, so please help! Thanks! John -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-30 20:02 UTC
Re: "You have a nil object" problem driving me crazy!
On 30 Nov 2008, at 19:58, John Doe wrote:> > > 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>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:>> > The obvious answer would be that you have a dilemma side with no side > images. > > FredHi 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/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-30 20:10 UTC
Re: "You have a nil object" problem driving me crazy!
On 30 Nov 2008, at 20:07, John Doe wrote:> > 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/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-30 20:12 UTC
Re: "You have a nil object" problem driving me crazy!
On 30 Nov 2008, at 20:07, John Doe wrote:> > 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> --- !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/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. > > FredHey, 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-Nov-30 20:28 UTC
Re: "You have a nil object" problem driving me crazy!
Hi -- On Sun, 30 Nov 2008, John Doe wrote:> > >> 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.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. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL See http://www.rubypal.com for details Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Could you maybe copy and paste it into pastie so that it is easier for everyone to look at? On Nov 30, 2:49 pm, John Doe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guys, The issue is solved. I dropped and repopulated my database, and it now works. I guess Fred was right after all, I was probably having a Side without an Image in one of my loops. Thanks for your time! I appreciate it. Kind regards, John -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---