4

I want to add the ability to read/write data to a CSV file to one of my models. In versions of ruby prior to 1.9 this would have been done with fasterCSV, but this is now part of ruby. I have the following setup:

#in my_model.rb
require 'CSV'

class MyModel < ActiveRecord::Base
   ... stuff ...
   def self.dump_to_csv(file=File.join(Rails.root, 'tmp', 'dump', 'my_model.csv'))
      CSV.open(file, "w") do |csv|
         keys = new.attributes.keys
         csv << keys
         all.each do |m|
            csv << m.attributes.values_at(*keys)
         end
      end
    end
end

This works fine, however when I come to run tests I get a load of warnings of the form

/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:201: warning: already initialized constant VERSION
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:863: warning: already initialized constant FieldInfo
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:866: warning: already initialized constant DateMatcher
... 

How can I remove these warnings?

Tom Close
  • 620
  • 5
  • 12

1 Answers1

8

I ran into the same issue, after some digging I realized that I was requiring 'CSV' where I should have been requiring 'csv' it should be all lowercase.

Claude
  • 96
  • 1
  • Holy WTF Batman. My rails WTFs / minute just went up. Thanks for answering this one. I don't have enough HP to solve this one today. – 23inhouse Sep 30 '13 at 01:34