Hi John,
> 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.
ActiveRecord doesn't support it, so you have to write your own methods
in your models in cases where you need it :
class Foo < ActiveRecord::Base
def many_rows( &block )
ActiveRecord::Base.connection.execute( sql ).each do |row|
yield row
end
end
end
Foo.new.many_rows do |r|
puts r.inspect
end
Note that ActiveRecord::Base.connection.execute returns a
MySQL::Result object, and MySQL::Result#each yields just an array -
You don't get the column names. That said, there might be a
#each_as_hash or something - I've not checked.
Using the method above, you're side-stepping ActiveRecord completely.
Sometimes that's necessary though, and you shouldn't feel dirty by
doing so :)
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
-~----------~----~----~----~------~----~------~--~---