Pilaf
2008-Oct-28 17:43 UTC
Iterating through all model records without pre-instantiating them
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
reHa
2008-Oct-28 21:22 UTC
Re: Iterating through all model records without pre-instantiating them
> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pilaf
2008-Oct-28 21:54 UTC
Re: Iterating through all model records without pre-instantiating them
Nice one, thanks! On Tue, Oct 28, 2008 at 7:22 PM, reHa <preszke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> >> 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. > > 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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---