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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.> 2) 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/1/12 Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:> 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. > >> 2) 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.