Forum: Ruby on Rails Iterating through all model records without pre-instantiating them

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1eaf88725aac486f742ae64a8abe0f6c?d=identicon&s=25 Pilaf (Guest)
on 2008-10-28 18:44
(Received via mailing list)

I'm writing a migration script which has to, well, actually migrate
all data from table A to table B, but I need to access the application
logic in model A for each row migrated. I could do this:

MyModel.all.each do |instance|
  # do stuff...

but as far as I know that would pre-instantiate all records in memory
beforehand, which could be *really bad* with the amount of records I'm
dealing with, and I only really need to work with a record at a time.
An alternative I'm currently using is:

execute("SELECT * FROM table_a").each_hash do |row|
  instance =

but I believe this is also sub-optimal (especially since some
attributes are attr_protected, therefore I'm having to take care of
those manually, and other reasons).

Is there any obvious/elegant way of doing this I'm missing?


5acf8e6be07793d22e24d8eb24a0e043?d=identicon&s=25 reHa (Guest)
on 2008-10-28 22:36
(Received via mailing list)
> dealing with, and I only really need to work with a record at a time.

Have you thought of using limit and a offset with a regular find?
(below is the code but I didn't check if it's valid)

current_offset = 0
limit = 100

  result = MyModel.find(:all, :limit=>limit, :offset=>current_offset)
  #some operations
  current_offset += limit
end while result.size == 0
1eaf88725aac486f742ae64a8abe0f6c?d=identicon&s=25 Pilaf (Guest)
on 2008-10-28 22:55
(Received via mailing list)
Nice one, thanks!
This topic is locked and can not be replied to.