# NOOB Question on Instance Variable

**URL:** <https://www.ruby-forum.com/t/noob-question-on-instance-variable/197119>\
**Category:** Rails\
**Created:** [January 12, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/noob-question-on-instance-variable/197119 "2010-01-12T20:20:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluehandtalking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@bluehandtalking](https://www.ruby-forum.com/u/bluehandtalking)\
**Post date:** [January 12, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/noob-question-on-instance-variable/197119/1 "2010-01-12T20:20:35Z")

</div>

I have a class photo. It belongs\_to a project.

In my ProjectsController I am creating 2 instance variables:

@displayphoto = Photo.find\_by\_project\_id(params[:id])

…and also

@photo = @project.build\_photo

The @displayphoto instance returns a nil object.  
However,

@photo = Photo.find\_by\_project\_id(params[:id])

works fine. So evidently it is the name @displayphoto  
that is causing the problem.

So I have 2 questions:

1. Must an instance variable always be the same  
name as the name of the class?
2. If the above is true, how can I create 2 separate  
instance variables in my controller for the same class?

Thanks,

Jet

---

<div class="post-metadata">

**Author:** ![bluehandtalking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@bluehandtalking](https://www.ruby-forum.com/u/bluehandtalking)\
**Post date:** [January 12, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/noob-question-on-instance-variable/197119/2 "2010-01-12T20:45:52Z")

</div>

Jet Thompson wrote:

> I have a class photo. It belongs\_to a project.
> 
> In my ProjectsController I am creating 2 instance variables:
> 
> @displayphoto = Photo.find\_by\_project\_id(params[:id])
> 
> …and also
> 
> @photo = @project.build\_photo
> 
> The @displayphoto instance returns a nil object.  
> However,
> 
> @photo = Photo.find\_by\_project\_id(params[:id])
> 
> works fine. So evidently it is the name @displayphoto  
> that is causing the problem.
> 
> So I have 2 questions:
> 
> 1. Must an instance variable always be the same  
> name as the name of the class?

No. An instance variable is just a variable (scoped to the instance of  
it’s class). And ivar can reference any object.

> 1. If the above is true, how can I create 2 separate  
> instance variables in my controller for the same class?

Just as you attempted to do. There is obviously something else going on  
here that can’t be seen from the code you provided. Or, the value of  
params[:id] is different between your two example.

---

<div class="post-metadata">

**Author:** ![bluehandtalking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@bluehandtalking](https://www.ruby-forum.com/u/bluehandtalking)\
**Post date:** [January 12, 2010, 9:45pm UTC](https://www.ruby-forum.com/t/noob-question-on-instance-variable/197119/3 "2010-01-12T21:45:29Z")

</div>

2010/1/12 Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> here that can’t be seen from the code you provided. Or, the value of  
> params[:id] is different between your two example.

One place to look for clues is in development.log, that will show you  
the params values and the queries run so you can check the id is  
correct.

If you still can’t find it have a look at the Rails Guide on debugging  
and break in to the failing point using ruby-debug to inspect the  
variables and see what is going on.

Colin
