Thanks for all your replies, they were very insightful.
The reason I was using SQL is because I didn't want all the active
record models loaded into the memory at once. I hadn't thought of
breaking the data into chunks using offsets, which would solve this
problem, I mean I could use pure Ruby. I refactored your solution John
to make something that could be added to any AR model

http://gist.github.com/14338

Which could then be used as

Customer.each_in_chunks_of{ |cust| # Do something with cust }

This doesn't allow for code like Comments.approved as in your example
though.

On Oct 2, 12:29 pm, John Leach <[EMAIL PROTECTED]> wrote:
> Oh, and I meant to mention, in comparison to my proposed solution, this
> one executes one query whereas mine executes (total number of rows /
> 100) queries.
>
> What's bad with mine is that MySQL does the query on the whole table on
> every loop and then just sends you the batch you asked for.  With a big
> table, this is really bad.
>
> Anyone know a good way of having ActiveRecord models do this kind of
> execute and fetch on each, rather than get the whole lot at the start?
> It's clearly preferable sometimes.
>
> John.
>
> On Thu, 2008-10-02 at 12:19 +0100, Carl Drinkwater | 29degrees wrote:
>
> > Hi John,
>
> > > Nice - that's a very tidy solution.  I'm assuming the each method on  
> > > res
> > > fetches the rows one at a time from the MySQL server, rather than
> > > grabbing them all at once.
>
> > Yeah, exactly.
>
> > Regards,
> > Carl.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to