简体   繁体   中英

Rails - CSV (export to csv)

i am exporting data to csv,

while exporting i want to split by every 50 records, instead of exporting all together.

(ie, if i click "Export to CSV" it should export first 50 records, later again on clicking "Export to csv" it should export next 50 records and so on)

please, provide me some code to solve this problem.

thanks

If pagination is not required you could try AR#find_in_batches .

Record.find_in_batches(:batch_size => 50) do |records|
  export_to_csv(records) # max 50 records
end
records = ModelClass.find(:limit => 50, ...)
# convert records to CSV

# later:
records = ModelClass.find(:limit => 50, :offset => 50, ...)

Looks like you want pagination (you do a 50 record per page).

There's a plugin for that: will_paginate

Then you do: Model.paginate :page => params[:page] , :per_page => 50

Then just add 1 to your page every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM