Search code examples
ruby-on-railspuma

Undefined method `to_ary' when rendering an Enumerator over Puma


When rendering large CSV documents, we are rendering an Enumerator which allows the browser to stream the document as its created. The Enumerator is passed to a controller render_csv method:

  def render_csv(body:, filename:)
    # Tell Rack to stream the content
    headers.delete("Content-Length")
    # Don't buffer when going through proxy servers
    headers["X-Accel-Buffering"] = "no"

    headers["Cache-Control"] = "no-cache"
    headers["Last-Modified"] = Time.now.httpdate
    headers["Content-Type"] = "text/csv; charset=utf-8; header=present"

    headers["Content-Disposition"] = %(attachment; filename="#{filename}.csv")
    self.response_body = body
  end

This used to work until Puma 6 which now raises undefined method `to_ary' on the enumerator being passed:

#<NoMethodError: undefined method `to_ary' for #<Enumerator: #<Enumerator::Generator:0x000000011e938568>:each>>

Has anyone run into this issue?


Solution

  • I started seeing this bug after upgrading to rails 7.1.1, it was fixed by upgrading to 7.1.2