NOOB Question on Instance Variable

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

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.

···

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

--
Iñaki Baz Castillo <ibc@aliax.net>

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.

···

On Jan 12, 12:17 pm, BlueHandTalking <j...@whidbey.com> wrote:

Phrogz,

thank you for answering my question:

> 1) Must an instance variable always be the same
> name as the name of the class?

No.

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 :slight_smile:

Jet

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

···

On Jan 12, 11:29 am, Iñaki Baz Castillo <i...@aliax.net> wrote:

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.

--
Iñaki Baz Castillo <i...@aliax.net>

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.

···

On Jan 12, 12:29 pm, Iñaki Baz Castillo <i...@aliax.net> wrote:

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.

*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?

···

On 12.01.2010 20:40, BlueHandTalking wrote:

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.

--
Phillip Gawlowski

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

···

El Martes, 12 de Enero de 2010, BlueHandTalking escribió:

Oh,

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

--
Iñaki Baz Castillo <ibc@aliax.net>

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. :slight_smile:

···

On 12.01.2010 20:45, BlueHandTalking wrote:

Oh,

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

--
Phillip Gawlowski

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

···

On 2010-01-12, BlueHandTalking <jet@whidbey.com> wrote:

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

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

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

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.

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.

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.

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.

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

···

On 12.01.2010 20:55, BlueHandTalking wrote:

--
Phillip Gawlowski

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

···

On Jan 12, 12:02 pm, Phillip Gawlowski <p...@thimian.com> wrote:

On 12.01.2010 20:55, BlueHandTalking wrote:

> 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.

>> 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.

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.

>> 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.

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

--
Phillip Gawlowski