Senthil Kumar Bhaskaran
Senthil Kumar Bhaskaran

Reputation: 7451

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.

(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

Upvotes: 0

Views: 2079

Answers (3)

amitkaz
amitkaz

Reputation: 2712

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.

Upvotes: 1

splattael
splattael

Reputation: 836

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

Upvotes: 2

lutz
lutz

Reputation:

records = ModelClass.find(:limit => 50, ...)
# convert records to CSV

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

Upvotes: 1

Related Questions