# Extending ActiveRecord for csv export... a better way?

**URL:** <https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835>\
**Category:** Rails\
**Created:** [October 31, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835 "2007-10-31T15:06:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [October 31, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/1 "2007-10-31T15:06:07Z")

</div>

## As a new Ruby user (JRuby, actually), I’ve picked up this language with a very specific purpose: to export data from a JDBC database into flat files. I’ve figured out how to get the data out of the DB and into a file, but it requires a bit too much configuration. My first stab implementation requires me to define an activerecord class for each table I want to export, and to define a method all\_columns in each class that lays out the columns in the correct order. I’ve also setup a module (CSVable) that extends ActiveRecord so I can call the export function directly on each class. Here is the code:

# Module definition

module CSVable  
extend ActiveRecord  
class Base \< ActiveRecord::Base  
def self.export\_table\_to\_csv(delimiter = “,”, options = nil)  
File.open("#{self.table\_name}.dat",  
File::WRONLY|File::TRUNC|File::CREAT) do |file|  
self.find(:all, options).each do |trow|  
file.puts trow.all\_columns.join(delimiter)  
end  
end  
end  
end  
end

# Class definitions

class USR\_USER\_ACCOUNT \< CSVable::Base  
set\_table\_name “USR.USER\_ACCOUNT”  
def all\_columns  
[ self.UID, self.USER\_NAME, self.USER\_INIT, self.ACTIVE\_FLAG,  
self.LAST\_MODFD\_TS ]  
end  
end

class GRP\_GRP \< CSVable::Base  
set\_table\_name “GRP.GRP”  
def all\_columns  
[ self.PARTY\_ROLE\_ASSIGN\_PID, self.GRP\_NBR\_ID,  
self.LAST\_MODIFIED\_TIMESTAMP ]  
end  
end  
…

# Usage for the module

## USR\_USER\_ACCOUNT.export\_table\_to\_csv("|")

The major problem with this approach is that I have to setup all the  
column info for each of the 36 tables. Is there an easier way to get an  
array of the columns while still retaining the original order?

I’ve tried using the attributes method, but since it returns a hash, the  
columns are not in order. My dabbles with the columns method have not  
been successful either. Any ideas?

I also have a feeling there is a much better way to implement the export  
function…

Any help or suggestions are most appreciated!

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [October 31, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/2 "2007-10-31T15:13:27Z")

</div>

Hi Adam

In ActiveScaffold, there is a very nice export plugin called  
activescaffoldxport.  
Check it out, it may help you …

CCH

> **[Rails... Rails... Rails](http://cch4rails.blogspot.com)**
>
> Chee Chong Hwa aka CCH, a Malaysian Chief Software Architect blogs on his experiences with Ruby on Rail running on Win2003 Server+Apache 2.26 environment since 2007. Flirted briefly with Ubuntu 9.04 in 2009. Since January 2017, now using RubyMine...

On Oct 31, 10:06 pm, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [October 31, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/3 "2007-10-31T15:23:36Z")

</div>

CCH wrote:

> Hi Adam
> 
> In ActiveScaffold, there is a very nice export plugin called  
> activescaffoldxport.  
> Check it out, it may help you …
> 
> CCH  
> [http://cch4rails.blogspot.com](http://cch4rails.blogspot.com)
> 
> On Oct 31, 10:06 pm, Adam B. \<removed\_email\_address@domain.invalid

Thanks for the tip, I’ll take a look. Is this a Ruby plugin or RoR?

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 2, 2007, 5:48am UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/4 "2007-11-02T05:48:55Z")

</div>

Pablo C. wrote:

> Try [http://rubyreports.org](http://rubyreports.org) …
> 
> On Oct 31, 8:06 am, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

If anyone else is looking for the solution to Ruby and JDBC exports, see  
the solution at [Extending ActiveRecord for csv export... a better way? - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/129963).

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 2, 2007, 6:02am UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/5 "2007-11-02T06:02:44Z")

</div>

Hi Adam

> > On Oct 31, 10:06 pm, Adam B. \<removed\_email\_address@domain.invalid
> 
> Thanks for the tip, I’ll take a look. Is this a Ruby plugin or RoR?

cch: It is a RoR plugin.

CCH

> **[Rails... Rails... Rails](http://cch4rails.blogspot.com)**
>
> Chee Chong Hwa aka CCH, a Malaysian Chief Software Architect blogs on his experiences with Ruby on Rail running on Win2003 Server+Apache 2.26 environment since 2007. Flirted briefly with Ubuntu 9.04 in 2009. Since January 2017, now using RubyMine...

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 2, 2007, 5:28am UTC](https://www.ruby-forum.com/t/extending-activerecord-for-csv-export-a-better-way/118835/6 "2007-11-02T05:28:17Z")

</div>

Try [http://rubyreports.org](http://rubyreports.org) …

On Oct 31, 8:06 am, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
