I needed to export some data from a huge data table. Iterating through MyModel.find took too long and too much ram, so I went straight to the db instead.

Example code was tough to find, so here's an example:
sql = "select * from #{ MyModel.table_name }"
MyModel.connection.execute(sql) do |handle|
  handle.fetch do |row|
    # row is an array of values
    ... export row ...
  end
end
blog comments powered by Disqus