CSV constant warning in Rails 1.2.2

by Matt Johnson Feb 15th, 2007 at 2:23 PM

After upgrading to Rails 1.2 we started seeing the following error in a Rails project that allows for downloading CSV files:

mime_type.rb:48: warning: already initialized constant CSV

Prior to 1.2 CSV was not supported as a mime type out of the box, so we had the following in our environment.rb which allowed us to serve the files using respond_to:

Mime::Type.register “text/csv”, :csv

Rails now has CSV as a pre-defined mime type as of 1.2, but a minor bug means you still need to define it yourself using something like the line above, which is what causes the warning to appear.

Luckily, by the time these warnings got midly annoying, someone else had already found a solution and it’s also fixed in Edge Rails.

It can be fixed in Rails 1.2.2 by replacing the Mime::Type.register line from environment.rb with the following:

Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV)

This will also be safely ignored if the bug is fixed for that release.