# Using object.find() in a loop not returning an array

**URL:** <https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072>\
**Category:** Rails\
**Created:** [January 24, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072 "2008-01-24T18:25:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 24, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072/1 "2008-01-24T18:25:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 24, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072/2 "2008-01-24T18:37:58Z")

</div>

On 24 Jan 2008, at 17:25, Corey M. 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 ])

Fred

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 24, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072/3 "2008-01-24T19:25:09Z")

</div>

Frederick C. wrote:

> On 24 Jan 2008, at 17:25, Corey M. 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 ])
> 
> 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 %\>

 \<%= scrs.id %\> \<%= scrs.title %\> \<% 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.

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 24, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072/4 "2008-01-24T20:03:13Z")

</div>

Nevermind I figured it out.

Thanks again for the help Fred.

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 24, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/using-object-find-in-a-loop-not-returning-an-array/127072/5 "2008-01-24T20:06:02Z")

</div>

On Jan 24, 2008, at 1:25 PM, Corey M. wrote:

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

> > 
> 
> error. My error is in how I’m attempting to access the values in the
> 
> 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 %\>

 \<%= scrn.id %\> \<%= scrn.title %\> \<% end %\>

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
