title: Is there a way to serialize an ActiveRecord Relation object without loading it?
body: <p>I am trying to export database rows to CSV depending on the parameters user picks. These exports can take a while, so I'm using delayed_job to process them and upload them to S3. My controller looks like this:</p>
  
  <pre><code>def search
    @books = visible_books.includes(:publisher)
    @books = @books.where(contract_id: assigned_contract_ids) if assigned_subscriber.present?
    @books = @books.unifilter(params, to_cents: ['total_cost'])
  
    respond_to do |format|
      format.json { export }
      format.js { @books.paginate(params[:page], per: 50) }
    end
  end
  
  def export
    exportable_books = ExportableBooks.new(@books)
    exportable_books.delay.upload
    render json: { id: exportable_books.id }
  end
  </code></pre>
  
  <p>Unfortunately, <code>@books</code> can hold up to 10.000 rows, and when they are serialized to YAML for delayed_job, all those rows get loaded into memory, and then written to the database in YAML format. Is there a way to avoid that?</p>