'No such file or directory @ rb_sysopen

I'm using Ruby 2.1.1 When I run this code:

<CSV.foreach("public/data/original/example_data.csv",headers: true, converters:              :numeric) do |info|

I get an error:

No such file or directory @ rb_sysopen

It works if I place example_data.csv in the same directory as shown below, but my boss said it can't be that way he wants all *.csv files in a different directory:

<CSV.foreach("example_data.csv",headers: true, converters: :numeric) do |info|


Solution 1:[1]

I had to use a workaround that bypassed File Utilities. Using thoughtbot/paperclip generated a directory called csvcontroller. I placed the csv file in that directory folder.

class Uploader < ActiveRecord::Base
attr_accessible :purchase_name, :item_description, :item_price, :purchase_count,
                  :merchant_address, :merchant_name, :csvdata

has_attached_file :csvdata, :url => "/csvcontroller/:basename.:extension",

                :path => ":rails_root/csvcontroller/:basename.:extension"

                #:default_url => "/controllers/original/example_data.csv"

  validates_attachment_content_type :csvdata, :content_type => ["text/csv"]

end

Then I placed my parser in that directory to avoid using FileUtils

require 'csv'

@total_cost = 0

#errors out FileUtils.move '/public/data/original/example_data.csv', '/controllers'

#errors out require File.expand_path('../app/public/data/original/', __FILE__)

# errors outCSV.foreach("any_path_name_outside_the_same_directory/example_data.csv", 
  #headers: true, converters: :numeric) do    |info|

CSV.foreach("example_data.csv", headers: true, converters: :numeric) do |info|

a =(info["item price"]).to_f

b = (info["purchase count"]).to_i

@total_cost += a * b
@store = []
customer = []
customer << info["purchaser name"]
@store << info["item description"]
@store << (info["item price"]).to_f
@store << (info["purchase count"]).to_i
@store << info["merchant address"]
@store << info["merchant name"]
puts @customer
puts @store
puts @total_cost
end

It looks ugly but that is what it is. I could not get the FileUtils:: class to work properly. This is a Ruby bug for 2.1.1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snowangel