Running a pop on a result set from a pgsql database I get:
undefined method `pop' for #<PG::Result:0x0000000273dc08>
I want to get the first 5 of that result set, do something with it, then do it again with the next 5. I don't want to run my query twice as it is a fairly long query.
How would I do this in ruby?
I am running ruby 2.1 and rails 3.0.
PG::Result
is Enumerable
so you could just use each_slice
on it:
your_pg_result.each_slice(5) do |array|
# array is at most five rows from the result set
# on each iteration so do whatever needs to be done
end
If you need to differentiate the iterations then throw a with_index
into the mix:
your_pg_result.each_slice(5).with_index do |array, i|
# ...
end