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:
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 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)
***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)
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 %>
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?
***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)
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 %>
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 %>