Scott Becker – How to generate CSV files in Rails

http://synthesis.sbecker.net/articles/2007/06/07/how-to-generate-csv-files-in-rails&hl=en&client=firefox-a&gl=us&strip=1/”>Synthesis

by Scott Becker

–>

How to generate CSV files in Rails 264

Posted by sbecker Thu, 07 Jun 2007 23:21:00 GMT

A while back someone posted on rubyonrails-talk asking how to export to CSV from Rails. I posted a solution, and people seemed to dig it, so I’ll share it again here.

Use FasterCSV

Get the FasterCSV gem. Why? It’s faster, and easier to use. Once you’ve got it, require it in environment.rb. Here’s an abbreviated version of my working controller method. Copy/paste/modify. And you’re done!

def export_to_csv  @users = User.find(:all)    csv_string = FasterCSV.generate do |csv|  # header row  csv << ["id", "first_name", "last_name"]    # data rows  @users.each do |user|  csv << [user.id, user.first_name, user.last_name]  end  end    # send it to the browsah  send_data csv_string,  :type => 'text/csv; charset=iso-8859-1; header=present',  :disposition => "attachment; filename=users.csv"  end

HTML or CSV, have it your way

Now, if we wanted to get all clever about it, we could go further and serve both html and CSV data with only one action, using respond_to. This also requires that you add a RESTful route (map.resources :users) to your routes.rb file:

def index  @users = User.find(:all)    respond_to do |wants|  wants.html  wants.csv do  csv_string = FasterCSV.generate do |csv|  # header row  csv << ["id", "first_name", "last_name"]    # data rows  @users.each do |user|  csv << [user.id, user.first_name, user.last_name]  end  end    # send it to the browsah  send_data csv_string,  :type => 'text/csv; charset=iso-8859-1; header=present',  :disposition => "attachment; filename=users.csv"  end  end    end  

Now if the user requests:

/users 

she’ll get HTML. If she requests:

/users.csv
via http://synthesis.sbecker.net/articles/2007/06/07/how-to-generate-csv-files-in-rails&hl=en&client=firefox-a&gl=us&strip=1″>74.125.155.132

[Saving this since the original seems to have gone away.]

Posted via web from crasch’s posterous

GEEK: Running a ruby script via crontab

So, I’m trying to set up a ruby script that screenscrapes another website, writes the results to a local file, then rsyncs the file to another machine behind a firewall. This post details the problems I ran into while trying to get cron to run the ruby script and rsync to the remote host.

(more…)

GEEK: Notes on rubygem, pdb.set_trace()

If you want to use a newly installed rubygems, and you don’t want to require ‘rubygems’ for every program that uses a gem, you need to set the RUBYOPT environment variable. For example, if you’re using tcsh, you should put the following in your .tcshrc file:

setenv RUBYOPT rubygems

This will cause the rubygems module to be loaded every time your run a ruby script.

——

If you call an external python script from within ruby, make sure you have any pdb.set_trace() lines commented out. When the ruby script tries to run the program, it will just hang at the point you set the trace, without giving you any output. For example, if test.py script has a pdb.set_trace() set, and you run the following command from within irb, the line will never finish executing. You’ll have to interrupt it with a CTRL-C command.

dfi_result = `/Users/build/crasch/PythonScripts/test.py -positions -total -infile /MData/Data/vyslf.csv -MStderrLoggingThreshold 3`

GEEK: how to add test cases to your ruby program

(more…)

GEEK: How to install ruby-dbi on cygwin

(more…)

GEEK: PostgreSQL connectivity on Cygwin

I found Steve Litt’s Ruby Database Connectivity article quite helpful for configuring ruby access to PostgreSQL on Cygwin. However, I ran into a couple of errors while running the sample files which I detail below:

(more…)

GEEK: ruby object methods

If you want a list of all a ruby object’s methods, try this:

puts object.public_methods.sort