NOOB Question on Instance Variable

Discussion in 'Ruby' started by BlueHandTalking, Jan 12, 2010.

  1. 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
    BlueHandTalking, Jan 12, 2010
    #1
    1. Advertising

  2. BlueHandTalking

    Phrogz Guest

    On Jan 12, 12:17 pm, BlueHandTalking <> 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.
    Phrogz, Jan 12, 2010
    #2
    1. Advertising

  3. 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 <>
    Iñaki Baz Castillo, Jan 12, 2010
    #3
  4. 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
    BlueHandTalking, Jan 12, 2010
    #4
  5. 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 <> 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 <>
    BlueHandTalking, Jan 12, 2010
    #5
  6. 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
    Phillip Gawlowski, Jan 12, 2010
    #6
  7. 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 <>
    Iñaki Baz Castillo, Jan 12, 2010
    #7
  8. 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
    Phillip Gawlowski, Jan 12, 2010
    #8
  9. 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
    BlueHandTalking, Jan 12, 2010
    #9
  10. 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
    Phillip Gawlowski, Jan 12, 2010
    #10
  11. 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 <> 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
    BlueHandTalking, Jan 12, 2010
    #11
  12. BlueHandTalking

    Phrogz Guest

    On Jan 12, 12:29 pm, Iñaki Baz Castillo <> 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.


    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.
    Phrogz, Jan 12, 2010
    #12
  13. BlueHandTalking

    Seebs Guest

    On 2010-01-12, BlueHandTalking <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Jan 14, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerry Sutton
    Replies:
    1
    Views:
    538
    Peter Otten
    Apr 16, 2005
  2. Arch Stanton

    Noob question - Need variable from DB field

    Arch Stanton, Feb 27, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    270
    Arch Stanton
    Feb 27, 2008
  3. David Garamond
    Replies:
    5
    Views:
    246
    Ara.T.Howard
    Jun 8, 2004
  4. Leon Bogaert
    Replies:
    19
    Views:
    327
    Robert Klemme
    Mar 23, 2008
  5. cayenne
    Replies:
    19
    Views:
    666
    John W. Krahn
    May 20, 2004
Loading...

Share This Page