Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > NOOB Question on Instance Variable

Reply
Thread Tools

NOOB Question on Instance Variable

 
 
BlueHandTalking
Guest
Posts: n/a
 
      01-12-2010
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
 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      01-12-2010
On Jan 12, 12:17*pm, BlueHandTalking <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      01-12-2010
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 <(E-Mail Removed)>

 
Reply With Quote
 
BlueHandTalking
Guest
Posts: n/a
 
      01-12-2010
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


Jet
 
Reply With Quote
 
BlueHandTalking
Guest
Posts: n/a
 
      01-12-2010
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 <(E-Mail Removed)> 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 <(E-Mail Removed)>


 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      01-12-2010
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.


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

--
Phillip Gawlowski

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      01-12-2010
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 <(E-Mail Removed)>

 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      01-12-2010
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?


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.

--
Phillip Gawlowski

 
Reply With Quote
 
BlueHandTalking
Guest
Posts: n/a
 
      01-12-2010
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

 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      01-12-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
noob question: Trying to extract part of a string in a variable to another variable cayenne Perl Misc 19 05-19-2004 11:22 PM



Advertisments