# Iterating through all model records without pre-instantiating them

**URL:** <https://www.ruby-forum.com/t/iterating-through-all-model-records-without-pre-instantiating-them/151984>\
**Category:** Rails\
**Created:** [October 28, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/iterating-through-all-model-records-without-pre-instantiating-them/151984 "2008-10-28T18:44:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pilaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecccb3/32.png) [@Pilaf](https://www.ruby-forum.com/u/Pilaf)\
**Post date:** [October 28, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/iterating-through-all-model-records-without-pre-instantiating-them/151984/1 "2008-10-28T18:44:24Z")

</div>

Hi,

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…

end

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 = MyModel.new(row)  
end

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?

Thanks,

-Pedro

---

<div class="post-metadata">

**Author:** ![Pilaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecccb3/32.png) [@Pilaf](https://www.ruby-forum.com/u/Pilaf)\
**Post date:** [October 28, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/iterating-through-all-model-records-without-pre-instantiating-them/151984/2 "2008-10-28T22:36:38Z")

</div>

> dealing with, and I only really need to work with a record at a time.  
> Hi,

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

begin  
result = MyModel.find(:all, :limit=\>limit, :offset=\>current\_offset)  
#some operations  
current\_offset += limit  
end while result.size == 0

---

<div class="post-metadata">

**Author:** ![Pilaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecccb3/32.png) [@Pilaf](https://www.ruby-forum.com/u/Pilaf)\
**Post date:** [October 28, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/iterating-through-all-model-records-without-pre-instantiating-them/151984/3 "2008-10-28T22:55:26Z")

</div>

Nice one, thanks!
