Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

CSV library error using WEBrick

#4861

Rails 3.0 beta4
Ruby 1.9.2preview3

uninitialized constant Invoice::CSV thrown when invoking CSV.generate in model using WEBrick.

The error does not occur when run through console.

require 'csv'

class Invoice < ActiveRecord::Base
  csv_content =  CSV.generate do |csv|
  
  end
end

Reported by Mark Winkler · June 14th, 2010 @ 07:03 PM

State: invalid
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • Milestone set to 3.x
    • Tag set to missing requires, rails 3, csv
    • Assigned user set to Pratik

    Confirmed on Rails master and Ruby 1.9.2-head.

    This is a very strange issue. I used CSV inside a class method on Post model. If I open the rails console, and try Post.foo, it works.

    However if I do reload! — it stops working.

    The model:

    require 'csv'
    
    class Post < ActiveRecord::Base
      def self.foo
        csv_content =  CSV.generate { |csv| }
      end
    end
    

    The console output:

    Loading development environment (Rails 3.0.0.beta4)
    ruby-1.9.2-head > Post.foo
     => "" 
    ruby-1.9.2-head > reload!
    Reloading...
     => true 
    ruby-1.9.2-head > Post.foo
    NameError: uninitialized constant Post::CSV
      from /home/rohit/bugmash/trythreadsafe/app/models/post.rb:5:in `foo'
      from (irb):3
      from /home/rohit/remote-repos/rails_patches/working4/railties/lib/rails/commands/console.rb:47:in `start'
      from /home/rohit/remote-repos/rails_patches/working4/railties/lib/rails/commands/console.rb:8:in `start'
      from /home/rohit/remote-repos/rails_patches/working4/railties/lib/rails/commands.rb:23:in `<top (required)>'
      from script/rails:6:in `require'
      from script/rails:6:in `<main>'
    

    Curiously enough, I tried the same thing with YAML, Digest and Zlib and things worked fine. I also tried on a Rails 2.x app on 1.8.7 and things worked.

    Can somebody please try on Rails 2.x and 1.9.2-preview3 or -head? Looks like a CSV lib issue on 1.9.2.

    June 15th, 2010 @ 03:56 AM

  2. Rohit Arondekar
    Rohit Arondekar

    Mark, can you try adding the require for csv inside config/application.rb ? After the Bundler.require should do fine. I think this should solve your issue. Please update the ticket if this solves your problem. :)

    June 19th, 2010 @ 08:54 AM

  3. Rizwan Reza
    Rizwan Reza
    • State changed from new to invalid

    June 19th, 2010 @ 05:21 PM

  4. Mark Winkler
    Mark Winkler

    Adding require for csv to config/application.rb solves it.

    June 19th, 2010 @ 06:19 PM