# Iterating through a database result

**URL:** <https://www.ruby-forum.com/t/iterating-through-a-database-result/151635>\
**Category:** Ruby\
**Created:** [October 23, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/iterating-through-a-database-result/151635 "2008-10-23T17:32:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 23, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/iterating-through-a-database-result/151635/1 "2008-10-23T17:32:10Z")

</div>

Hi all,

I’ve stumbled again. I’m currently trying to iterate though a result  
output from PostgreSQL. Here’s the database; I’ve been able to query  
that I am getting all 5 records, however I don’t seem to be able to  
iterate through the second column i.e. 1,2,3,4,5.

7494;“1”;“The origin (0,0) is top left.”;TRUE;“1.0”;“NOTE: Question  
imported from map”;“Correct.”;;

7494;“2”;“The origin (0,0) is top  
right.”;FALSE;"-0.25";"";“Incorrect.”;;

7494;“3”;“The origin (0,0) is bottom  
left.”;FALSE;"-0.25";"";“Incorrect.”;;

7494;“4”;“The origin (0,0) is bottom  
right.”;FALSE;"-0.25";"";“Incorrect.”;;

7494;“5”;“The origin (0,0) is located in the centre of the  
screen.”;FALSE;"-0.25";"";“Incorrect.”;;

What I’m currently using is:

num\_options = res\_qo.to\_a.length

puts num\_options

puts res\_qo.to\_a

for i in (1…num\_options)

```
puts res_qo.to_ary[0][1]

```

end

The result is an array of arrays by the looks of it (unless someone can  
point out I’ve got the wrong end of the stick), but I can’t move on from  
the first record and am currently getting:

1

1

1

1

1

Any ideas would be great, as this should be one of the last problems I  
have to sort out for this migration script.

Cheers,

Dan

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 23, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/iterating-through-a-database-result/151635/2 "2008-10-23T17:38:01Z")

</div>

On Fri, 24 Oct 2008, Daniel Malcolm Webb [dbw] wrote:

> imported from map";“Correct.”;;
> 
> 7494;“2”;“The origin (0,0) is top  
> right.”;FALSE;"-0.25";"";“Incorrect.”;;

```
    [...]

```

> ```
> puts res_qo.to_ary[0][1]
> 
> ```

```
  puts res_qo.to_a[i][1]
```

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 23, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/iterating-through-a-database-result/151635/3 "2008-10-23T17:53:16Z")

</div>

On Thu, Oct 23, 2008 at 11:15 PM, Daniel Malcolm Webb [dbw]  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> What I’m currently using is:  
> num\_options = res\_qo.to\_a.length  
> puts num\_options  
> puts res\_qo.to\_a  
> for i in (1…num\_options)  
> puts res\_qo.to\_ary[0][1]  
> ^^^^^^^  
> i think that should be [i][1]  
> end

also, you are reconverting req\_qo to array in each loop when in fact  
you’ve already created it at the beginning

try

res\_qo.to\_a.each do |row|  
puts row[1]  
end

kind regards -botp

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 24, 2008, 10:20am UTC](https://www.ruby-forum.com/t/iterating-through-a-database-result/151635/4 "2008-10-24T10:20:43Z")

</div>

ah brilliant that worked. Thank you very much 🙂
