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

2

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.