# Taking Any Returned Record Set and Writing It In CVS Format

**URL:** <https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276>\
**Category:** Rails\
**Created:** [December 13, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276 "2006-12-13T19:53:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/1 "2006-12-13T19:53:15Z")

</div>

I would like to be able to do the following in my controller:

@job = Job.find(:all)  
write\_returned\_recordset\_to\_file(@job, “filename.txt”)

And have the write\_returned\_recordset\_to\_file write the returned data  
with column headers to a file. I have some idea but they are not  
class independent. Any sexy suggestions?

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/2 "2006-12-13T20:29:20Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

John K. wrote:

> I would like to be able to do the following in my controller:
> 
> @job = Job.find(:all)  
> write\_returned\_recordset\_to\_file(@job, “filename.txt”)
> 
> And have the write\_returned\_recordset\_to\_file write the returned data  
> with column headers to a file. I have some idea but they are not  
> class independent. Any sexy suggestions?

How about this?

require ‘faster\_csv’

module FindToCSV  
def self.included( cl )  
cl.instance\_eval "  
alias :\_original\_find :find  
def find( \*args )  
results = \_original\_find( \*args )  
results.extend( ArrayExtension ) if results.is\_a?( Array )  
results  
end  
"  
end

module ArrayExtension  
def to\_csv( filename )  
csv = FasterCSV.generate do |csv|  
csv \<\< self.first.attributes.sort.map{ |arr| arr.first }  
self.each do |row|  
csv \<\< self.first.attributes.sort.map{ |arr| arr.last }  
end  
end  
File.open( filename, “w” ){ |io| io.puts csv }  
end  
end  
end

class Job \< ActiveRecord::Base  
include FindToCSV  
end

@jobs = Job.find( :all )  
@jobs.to\_csv( ‘myfile.csv’ )

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFgFR8Myx0fW1d8G0RAufzAJ4s5MIenAtu/IvKKttyExfaDtSL4QCfexu3  
ayht6w29hQaoy5anazy/+z0=  
=+ECB  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/3 "2006-12-13T20:37:23Z")

</div>

definitely a faq item - CSV (common separated values export)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/4 "2006-12-13T21:05:30Z")

</div>

referring to this proposal:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/)  
99ea153cc378f7f1/ba010c2c547d7637?lnk=gst&q=%5BFAQ%  
5D&rnum=2#ba010c2c547d7637

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/5 "2006-12-13T21:49:36Z")

</div>

I really like this, but this would be nice too

headers = [“id” , “name” , …]  
exclude = [“phone\_number” , “user\_id”]  
@jobs.to\_csv( ‘myfile.csv’ , :header=\> headers , :exclude\_cols=\>  
exclue )

so I can add headers to the csv file, and also exclude certain columns  
easily

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/6 "2006-12-13T20:55:29Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Jodi S. wrote:

> definitely a faq item - CSV (common separated values export)

I am going to add this to the ActiveRecord::Extensions plugin… where  
is this FAQ page that you speak?

[http://www.continuousthinking.com/are](http://www.continuousthinking.com/are)

Zach

> > > > with column headers to a file. I have some idea but they are not  
> > > > results = \_original\_find( \*args )  
> > > > self.each do |row|  
> > > > include FindToCSV  
> > > > end
> 
> @jobs = Job.find( :all )  
> @jobs.to\_csv( ‘myfile.csv’ )
> 
> Zach
> 
> >

> 

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFgFqjMyx0fW1d8G0RAulQAJ0WbwEETGthP9OvAps7Llo9CxyWGACfUYV8  
OkRYyGRGTJrG+8ytdylOIWI=  
=V6X6  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 13, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/taking-any-returned-record-set-and-writing-it-in-cvs-format/79276/7 "2006-12-13T22:45:50Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

removed\_email\_address@domain.invalid wrote:

> I really like this, but this would be nice too
> 
> headers = [“id” , “name” , …]  
> exclude = [“phone\_number” , “user\_id”]  
> @jobs.to\_csv( ‘myfile.csv’ , :header=\> headers , :exclude\_cols=\>  
> exclue )
> 
> so I can add headers to the csv file, and also exclude certain columns  
> easily

Ok, i just committed the first rendition to my svn repos for  
ActiveRecored::Extensions with an initial test. I’ll add some test  
cases for this later today and get the functionality committed.

This weekend ActiveRecord::Extensions will launch a new release so if  
you can hold out until then you’ll be able to install it as  
a plugin or as a gem. If not feel free to pull down from trunk,  
[http://rubyforge.org/projects/arext/](http://rubyforge.org/projects/arext/)

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFgHR4Myx0fW1d8G0RAhxFAJsECzyvsbhhwBaTNyn3P82ko2jd2ACfVtSc  
XmNcOf8kvL3atwmpEwel3ek=  
=+jtQ  
-----END PGP SIGNATURE-----
