If you want to do this in ActiveRecord, you wont have much luck. I
worked on some code to provide functionality like you want but there are
class CoolBob < ActiveRecord::Base
CoolBob.each do |bob|
// DO something with each record (as it is returned from the adapter)
In this implementation, only 1 CoolBob object is created, and its
@attributes are updated with each row fetch. The problem here is that
Ruby being a GC’d language, it doesnt save you anything OTHER than
object creation time since it doesnt create a new object with each row.
It doesn’t save on any memory use really. Each @attributes (which is the
row data as an array) will still use memory until collected. So not much
different than creating all the objects up front.
Also, the Ruby MySQL adapter (as well as others) doesn’t support
unbuffered queries which is where you would get your speed increase and
judicial use of memory from anyways. So in short, what you want to do
isn’t really possible using AR, but is possible using the method in the
previous post, even though that still buffers the complete result set
before being given access to it.