I am trying to get ALL the filtered results when exporting to CSV.
My to_csv
is working fine, and I suspect something to do with my controller.
Search around with kaminari, ransack, but seems like even exporting to csv is rare using ransack.
Any help is much appreciated.
controller.rb
@search = Order.includes(:user, :order_items).ransack(params[:q])
@orders = @search.result.order(created_at: :desc).page(params[:page])
respond_to do
|format|
format.html
format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
end
view.html.haml
= link_to 'Download as CSV', a_o_path(request.params.merge(format: 'csv')), { class: 'btn btn-primary', style: 'float: right;' }
You can do this with ransack and kaminari, but you'll need to update your controller just a bit. Here's what you have so far:
format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
When your controller gets here, @orders
has been filtered by ransack, but it has also been paginated by kaminari. Since you're responding with html in some cases and csv in others, you'll want to do something slightly different when you respond with csv.
Here's what I would try:
@search = Order.includes(:user, :order_items).ransack(params[:q])
@orders = @search.result.order(created_at: :desc)
respond_to do |format|
format.html { @orders = @orders.page(params[:page]) }
format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
end
Basically, you only paginate the query when you respond with html. When csv is requested, all of the orders will still be there.