I''m running a loop (illustrated below) that retrieves records from a table based on an id that might be different with each iteration. The loop uses the array scrns (composed of IDs) to find the related records in the Screens table. I''m attempting to store the returned results in an instance variable so I can use that data in a view. However, this code simply is overwriting the instance variable with each iteration. Why? I want to end up with an array or hash in the instance var so I can iterate through it in my view to display. ***CODE**** for scr in scrns @scrs = Screen.find(:all, :conditions => [ "id = ?", scr.screen_id ]) end ***CODE**** I''ve tried modifying the statement above in multiple ways: ***CODE**** @scrs << Screen.find( blah blah @scrs += Screen.find( blah blah Screen.find( blah blah).collect blah ***CODE**** Nothing is working. Any ideas? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-24 17:36 UTC
Re: using object.find() in a loop not returning an array
On 24 Jan 2008, at 17:25, Corey Murphy wrote:> > I''m running a loop (illustrated below) that retrieves records from a > table based on an id that might be different with each iteration. The > loop uses the array scrns (composed of IDs) to find the related > records > in the Screens table. I''m attempting to store the returned results in > an instance variable so I can use that data in a view. However, this > code simply is overwriting the instance variable with each iteration. > Why? I want to end up with an array or hash in the instance var so I > can iterate through it in my view to display. > > ***CODE**** > for scr in scrns > @scrs = Screen.find(:all, :conditions => [ "id = ?", scr.screen_id ]) > end > ***CODE**** > > I''ve tried modifying the statement above in multiple ways: >This is all pretty much basic ruby. You''ll save yourself a lot of frustration by reading up on this sort of stuff and getting well acquainted with ruby itself.> ***CODE**** > @scrs << Screen.find( blah blahwould work if you initially set @scrs to []> > @scrs += Screen.find( blah blahwould also work if you initially set @scrs to [], but wouldn''t do the same thing as <<. (I''m guessing the previous version does what you actually want)> Screen.find( blah blah).collect blahscrns.collect {|scr| Screen.find :all, :conditions =>[ "id = ?", scr.screen_id ]) Fred> > ***CODE**** > > Nothing is working. Any ideas? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Corey Murphy
2008-Jan-24 18:25 UTC
Re: using object.find() in a loop not returning an array
Frederick Cheung wrote:> On 24 Jan 2008, at 17:25, Corey Murphy wrote: > >> >> ***CODE**** >> for scr in scrns >> @scrs = Screen.find(:all, :conditions => [ "id = ?", scr.screen_id ]) >> end >> ***CODE**** >> >> I''ve tried modifying the statement above in multiple ways: >> > This is all pretty much basic ruby. You''ll save yourself a lot of > frustration by reading up on this sort of stuff and getting well > acquainted with ruby itself. >> ***CODE**** >> @scrs << Screen.find( blah blah > would work if you initially set @scrs to [] >> >> @scrs += Screen.find( blah blah > would also work if you initially set @scrs to [], but wouldn''t do the > same thing as <<. (I''m guessing the previous version does what you > actually want) > >> Screen.find( blah blah).collect blah > > scrns.collect {|scr| Screen.find :all, :conditions =>[ "id = ?", > scr.screen_id ]) > > FredThanks Fred, Good to know I''m not going crazy. I had tried your first suggestion last night, but I''ve apparently not been looking carefully enough at my error. My error is in how I''m attempting to access the values in the view. However treating @scrs as an array and then iterating through it is still returning me a error of the following: ***CODE*** <% for scrs in @scrs %> <tr> <td><%= scrs.id %></td> <td><%= scrs.title %></td> </tr> <% end %> ***ERROR*** undefined method `title'' for [#<Screen id: 2, title: "Test Screen Title">]:Array I''m assuming I have an array @scrs which contains a hash for each record that was stored in each array indice. So how would I go about accessing a hash key value pair from within an array? scrs[''title''] maybe? OR scrs[:title] Thanks again for your help. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Corey Murphy
2008-Jan-24 19:03 UTC
Re: using object.find() in a loop not returning an array
Nevermind I figured it out. Thanks again for the help Fred. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Jan-24 19:05 UTC
Re: using object.find() in a loop not returning an array
On Jan 24, 2008, at 1:25 PM, Corey Murphy wrote:> Frederick Cheung wrote: >> On 24 Jan 2008, at 17:25, Corey Murphy wrote: >>> ***CODE**** >>> for scr in scrns >>> @scrs = Screen.find(:all, :conditions => [ "id = ?", >>> scr.screen_id ]) >>> end >>> ***CODE**** >>> >>> I''ve tried modifying the statement above in multiple ways: >>> >> This is all pretty much basic ruby. You''ll save yourself a lot of >> frustration by reading up on this sort of stuff and getting well >> acquainted with ruby itself.If scrns looks something like [ 123, 456, 987, 534 ], that is, a simple array of integers, then you just need to say: @scrs = Screen.find(scrns)>>> ***CODE**** >>> @scrs << Screen.find( blah blah >> would work if you initially set @scrs to [] >>> >>> @scrs += Screen.find( blah blah >> would also work if you initially set @scrs to [], but wouldn''t do the >> same thing as <<. (I''m guessing the previous version does what you >> actually want) >> >>> Screen.find( blah blah).collect blah >> >> scrns.collect {|scr| Screen.find :all, :conditions =>[ "id = ?", >> scr.screen_id ]) >> >> Fred > > Thanks Fred, > > Good to know I''m not going crazy. I had tried your first suggestion > last night, but I''ve apparently not been looking carefully enough at > my > error. My error is in how I''m attempting to access the values in the > view. However treating @scrs as an array and then iterating through it > is still returning me a error of the following: > > ***CODE*** > <% for scrs in @scrs %> > <tr> > <td><%= scrs.id %></td> > <td><%= scrs.title %></td> > </tr> > <% end %> > > ***ERROR*** > undefined method `title'' for [#<Screen id: 2, title: "Test Screen > Title">]:Array > > I''m assuming I have an array @scrs which contains a hash for each > record > that was stored in each array indice. So how would I go about > accessing > a hash key value pair from within an array? > > scrs[''title''] maybe? > OR > scrs[:title] > > Thanks again for your help.I think you just need to try my first suggestion (if the assumption is accurate). Since find(:all, ...) is going to return something that behaves like an array, you probably need to flatten the results: <% for scrn in @scrs.flatten %> <tr> <td><%= scrn.id %></td> <td><%= scrn.title %></td> </tr> <% end %> -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---