# You have a nil object when you didn't expect it!

**URL:** <https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/131563>\
**Category:** Rails\
**Created:** [March 11, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/131563 "2008-03-11T12:06:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 11, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/131563/1 "2008-03-11T12:06:08Z")

</div>

Im trying to get information from a database and display it on a view, I  
am using this in the controller:

def comment  
@comments = Rating.find(:all)

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @comments }
end

```

end

and this to display the information

\<% for comment in @comments %\>  
\<%=h comment.item\_name %\>  
\<%=h comment.comment %\>  
\<%=h comment.seller\_rating %\>  
\<%=h comment.advert\_rating %\>  
\<% end %\>

Yet I get an error saying i have a nil object, can anyone help me  
please?

---

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 11, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/131563/2 "2008-03-11T12:15:49Z")

</div>

On 11 Mar 2008, at 11:06, Alan Red wrote:

> Im trying to get information from a database and display it on a  
> view, I  
> am using this in the controller:

The error should include a stack trace which should tell you on which  
line it’s happening (and this which object is (unexpectedly) nil.

Fred

---

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 11, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/131563/3 "2008-03-11T16:56:05Z")

</div>

I agree with Fred, but my wild guess is going to be that you’ve got a  
view that is geared up for Comments but a controller that’s retrieving  
a Rating. Do you need to do something more like  
Comment.find(params[:id], :include=\>:ratings)?
