0

i am exporting data to csv,

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

(i.e, 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

Senthil Kumar Bhaskaran
  • 7,371
  • 9
  • 43
  • 56

3 Answers3

2

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
splattael
  • 806
  • 7
  • 4
1
records = ModelClass.find(:limit => 50, ...)
# convert records to CSV

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

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
amitkaz
  • 2,732
  • 1
  • 20
  • 18