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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With