将已删除的记录从数据库存储到CSV

时间:2015-07-15 08:37:17

标签: ruby-on-rails ruby csv sinatra

有近100万条用户记录,因此我们删除非活动用户以保持表一致性。我们正在使用Sinatra框架。

在我们的模型中:

class UserInfo < ActiveRecord::Base
  def delete_expired_users
    begin
      @expired_users = UserInfo.where("status != ? and UEndDate < ?", "active", 3.months.ago )      

      @expired_users.find_each do |user|  # as we have 500k records to delete, used "find_each" IS THIS ASYNCHRONOUS WAY TO DELETE?
        user.destroy
        # I NEED TO STORE THIS DESTROYED USER IN A CSV FILE.
      end

    rescue
      raise
    end
  end
end

0 个答案:

没有答案