Join table query error

Hi,
I’m learning Rails and am getting an error following a query. I think
I’m missing something simple and would appreciate guidance.

In controller:
@pictures = Question.find(session[:question_id]).pictures # Query
through the join table

When I look at @picture :
[#<Picture id: 44, title: “”, remote_image_url: nil, contributor: “”,
created_at: “2013-04-10 11:16:34”, updated_at: “2013-04-10 11:16:34”,
gallery_id: nil, image: “pict4.png”>, #<Picture id: 46, title: “”,
remote_image_url: nil, contributor: “”, created_at: “2013-04-12
23:39:18”, updated_at: “2013-04-12 23:39:18”, gallery_id: nil, image:
“pict2.jpg”>]

In view:
<% @pictures.each do |picture| %>
<%= image_tag(@picture.image %>
<% end %>

Error: undefined method `image’ for nil:NilClass

Thanks

The code in your view should be:

<% @pictures.each do |picture| %>
<%= image_tag(picture.image %>
<% end %>

without @

2013/4/12 Dave C. [email protected]

created_at: “2013-04-10 11:16:34”, updated_at: “2013-04-10 11:16:34”,
Error: undefined method `image’ for nil:NilClass
email to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Ricardo Franco Andrade

( Web | Desktop | Game ) Developer

email: [email protected]
phone: +55 (86) 9436 0830
linkedin: http://www.linkedin.com/pub/ricardo-franco/61/2b0/857
skype: ricardo.krieg
github: ricardokrieg (Ricardo Andrade) · GitHub
twitter: twitter.com/ricardokrieg
facebooK: Redirecting...

Also I’m not sure what Question is, but your picture model seems to be
missing a question_id… so your
Question.find(session(:question_id).pictures will always be blank, won’t
it?

Julian

Ricardo Franco wrote in post #1105491:

The code in your view should be:

<% @pictures.each do |picture| %>
<%= image_tag(picture.image %>
<% end %>

without @

Thank you Ricardo!
(Must have been too tired…)

Julian L. wrote in post #1105496:

Also I’m not sure what Question is, but your picture model seems to be
missing a question_id… so your
Question.find(session(:question_id).pictures will always be blank, won’t
it?

Julian

The query is to find all pictures with the question id of
session[:question_id] from a join table, so works fine.

Thanks you both for reply!

Dave