NOOB Question on Instance Variable

B

BlueHandTalking

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
 
P

Phrogz

In my ProjectsController I am creating 2 instance variables:
Looks like you're talking about Ruby on Rails, not Ruby the language.
@displayphoto = Photo.find_by_project_id(params[:id])
And specifically here you appear to be using ActiveRecord.
The @displayphoto instance returns a nil object.
That would appear to be because find_by_project_id is returning nil,
given the code you've shown.
@photo = Photo.find_by_project_id(params[:id])
works fine. So evidently it is the name @displayphoto
that is causing the problem.
I believe that you are not testing what you think you are testing.
Where are you later asking for the value of @photo versus
@displayphoto. It is extremely unlikely that what you think is
happening is true.
1)  Must an instance variable always be the same
     name as the name of the class?
No.
 
I

Iñaki Baz Castillo

El Martes, 12 de Enero de 2010, BlueHandTalking escribi=F3:
I have a class photo. It belongs_to a project.
=20
In my ProjectsController I am creating 2 instance variables:
=20
@displayphoto =3D Photo.find_by_project_id(params[:id])
=20
...and also
=20
@photo =3D @project.build_photo


Hi, this is a list of Ruby and your question is related to RubyOnRails that=
is=20
a *web framework* coded in Ruby.
Please ask such question in a RubyOnRails maillist.

=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
B

BlueHandTalking

Phrogz,

thank you for answering my question:

In regard to the below:
@displayphoto = Photo.find_by_project_id(params[:id])
The @displayphoto instance returns a nil object.


I know that your response is incorrect in regard to
That would appear to be because find_by_project_id is returning nil,
given the code you've shown.

....because the same exact code works correctly with
the name being @photo instead of @displayphoto.


And for the below:
I believe that you are not testing what you think you are testing.
Where are you later asking for the value of @photo versus
@displayphoto. It is extremely unlikely that what you think is
happening is true.

That could be true, something is definitely wrong somewhere :)


Jet
 
B

BlueHandTalking

Oh,

I thought it was based on Ruby, and Rails is written in
Ruby that it fit into the category of being about Ruby?

Jet

El Martes, 12 de Enero de 2010, BlueHandTalking escribió:
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

Hi, this is a list of Ruby and your question is related to RubyOnRails that is
a *web framework* coded in Ruby.
Please ask such question in a RubyOnRails maillist.
 
P

Phillip Gawlowski

I know that your response is incorrect in regard to


...because the same exact code works correctly with
the name being @photo instead of @displayphoto.

*Is* it the exact same code? Do you search for @displayphoto before or
after doing a #build_photo call? Do you *have* records in your database
to search for and thus get a result set?
 
I

Iñaki Baz Castillo

El Martes, 12 de Enero de 2010, BlueHandTalking escribi=F3:
Oh,
=20
I thought it was based on Ruby, and Rails is written in
Ruby that it fit into the category of being about Ruby?

Would you ask about Apache custom modules in a maillist of C++?


=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
P

Phillip Gawlowski

Oh,

I thought it was based on Ruby, and Rails is written in
Ruby that it fit into the category of being about Ruby?

It's a generally Ruby-related question. Your issue isn't necessarily
Rails specific. However, you get much higher success rates on Rails
related fora, than on ruby-talk. After all, every Rails user uses Ruby
(to an extend, anyway), but not every Ruby user users Rails. :)
 
B

BlueHandTalking

Hello Phillip,
*Is* it the exact same code?
Yes,

Do you search for @displayphoto before or
after doing a #build_photo call?

Not sure what you mean by doing a search for @displayphoto,
but I am using @displayphoto in my code, if thats what you meand.
Do you *have* records in your database
to search for and thus get a result set?

Yes, as I mentioned in my first post, everything works
correctly if the instance is named @photo.

I change code in the class and where I call it in my
application when I test.

Jet
 
P

Phillip Gawlowski

Hello Phillip,
*Is* it the exact same code?
Yes,
[...]
I change code in the class and where I call it in my
application when I test.

Obviously, the code is different then, is it not?

To clarify: If we ask for exactness, we *mean* exactness. No changes, no
nothing, but 100% identical code.
Not sure what you mean by doing a search for @displayphoto,
but I am using @displayphoto in my code, if thats what you meand.

So, you do a DB search for @displayrecords before actually creating a DB
record, and are surprised that that search returns nil? Then you create
a DB record, and are surprised that you find something?

The name of an instance variable is utterly irrelevant. You can
substitute @displayphoto with @foo, and @photo with @bar, and still get
the same results.
Yes, as I mentioned in my first post, everything works
correctly if the instance is named @photo.

Not an answer. Are there actual records in the database that you can
find? Before creating a record in your code?
 
B

BlueHandTalking

Hi there Phil,

Yes everything was exact.

I did once more check out my record I was
testing with, and found out that the project_id
had been deleted in the middle of my tests, and
that was the error.

Thanks again for all the help.

Cheers,

Jet

Hello Phillip,
*Is* it the exact same code?
[...]
I change code in the class and where I call it in my
application when I test.

Obviously, the code is different then, is it not?

To clarify: If we ask for exactness, we *mean* exactness. No changes, no
nothing, but 100% identical code.
Not sure what you mean by doing a search for @displayphoto,
but I am using @displayphoto in my code, if thats what you meand.

So, you do a DB search for @displayrecords before actually creating a DB
record, and are surprised that that search returns nil? Then you create
a DB record, and are surprised that you find something?

The name of an instance variable is utterly irrelevant. You can
substitute @displayphoto with @foo, and @photo with @bar, and still get
the same results.
Yes, as I mentioned in my first post, everything works
correctly if the instance is named @photo.

Not an answer. Are there actual records in the database that you can
find? Before creating a record in your code?
 
P

Phrogz

Hi, this is a list of Ruby and your question is related to RubyOnRails that is
a *web framework* coded in Ruby.
Please ask such question in a RubyOnRails maillist.

In his defense, Iñaki, he was asking about assigning values to
instance variables, which has nothing to do with Rails. Yes, he was
using Rails/AR, and yes, the problem turned out to be DB/test related,
but he actually question was valid, I think, for a general Ruby
discussion.
 
S

Seebs

I thought it was based on Ruby, and Rails is written in
Ruby that it fit into the category of being about Ruby?

There are rails-related questions that are Ruby questions, but not all
that many.

Lemme give you an example. Say you know C.

I quote this code:
normalize(&r.start);

and ask you if you can explain why that's "r.start" rather than "r.end".

What are your chances?

Hint: Roughly zero. Because that's actually a question about a particular
program, rather than a question abuot the language.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top