# Export to CSV,

**URL:** <https://www.ruby-forum.com/t/export-to-csv/174784>\
**Category:** Rails\
**Created:** [September 2, 2009, 7:05am UTC](https://www.ruby-forum.com/t/export-to-csv/174784 "2009-09-02T07:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@lamrin](https://www.ruby-forum.com/u/lamrin)\
**Post date:** [September 2, 2009, 7:05am UTC](https://www.ruby-forum.com/t/export-to-csv/174784/1 "2009-09-02T07:05:49Z")

</div>

in Export to CSV, i have overall 2500 records, and while exporting it  
takes long time to export all records, so, i have decided to export in  
the form of 1st 50 students,and, 2nd 50 students,so on. I have tried  
the below code. but it could able to fetch only 1st 50 students.

please, guide me how to solve the problem

@student\_count = Student.find(:all)  
@count1 = @student\_count.count  
st\_per\_file = 50  
count = 0  
unless @count1==count  
students = Student.find(:all, :order =\> ‘name’, :limit =\>  
st\_per\_file, :offset =\> (st\_per\_file\*count))  
count = count + 1  
end

---

<div class="post-metadata">

**Author:** ![lamrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@lamrin](https://www.ruby-forum.com/u/lamrin)\
**Post date:** [September 2, 2009, 7:15am UTC](https://www.ruby-forum.com/t/export-to-csv/174784/2 "2009-09-02T07:15:28Z")

</div>

do something like this

Student.all(:order =\> ‘name’).in\_groups\_of(50, false).each\_with\_index  
do |group,index|  
export\_to\_csv(“file\_number#{index}.csv”, group)  
end

see

> <https://stackoverflow.com/questions/1360682/rails-csvexport-to-csv-loop/1361155#1361155>

for more info regarding the exact same question

Adam

---

<div class="post-metadata">

**Author:** ![lamrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@lamrin](https://www.ruby-forum.com/u/lamrin)\
**Post date:** [September 2, 2009, 7:18am UTC](https://www.ruby-forum.com/t/export-to-csv/174784/3 "2009-09-02T07:18:23Z")

</div>

lamrin Pn wrote:

> in Export to CSV, i have overall 2500 records, and while exporting it  
> takes long time to export all records, so, i have decided to export in  
> the form of 1st 50 students,and, 2nd 50 students,so on. I have tried  
> the below code. but it could able to fetch only 1st 50 students.
> 
> please, guide me how to solve the problem
> 
> @student\_count = Student.find(:all)  
> @count1 = @student\_count.count  
> st\_per\_file = 50  
> count = 0  
> unless @count1==count  
> students = Student.find(:all, :order =\> ‘name’, :limit =\>  
> st\_per\_file, :offset =\> (st\_per\_file\*count))  
> count = count + 1  
> end

```
 filename = 'students.csv'
  headers.merge!(
    'Content-Type' => 'text/csv',
    'Content-Disposition' => "attachment; filename=\"#{filename}\"",
    'Content-Transfer-Encoding' => 'binary'
  )
```
