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

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?

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

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.

Nevermind I figured it out.

Thanks again for the help Fred.

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
[email protected]