# Feed an .csv file up to server for importing data, then throw away?

**URL:** <https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189>\
**Category:** Rails\
**Created:** [April 29, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189 "2008-04-29T18:04:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189/1 "2008-04-29T18:04:27Z")

</div>

I’ve written a routine allowing the client user to declare where  
their .csv import file resides and then send it to the server to  
import the many rows into an existing model table, and then throw it  
away.  
I’ve imported many graphic files into my application using  
attachment\_fu and this was very straightforward as they were imported  
into an existing model object. Instead, I am trying to import a file,  
that when it’s processed will be ‘thrown away’. These examples are  
running ok, but I won’t be able to use this strategy in my production  
environment…  
Here’s my view code:

\<%= form\_tag(:url =\> {:action=\> “post” }, :html =\> { :multipart =\>  
true }) -%\>

\<%= file\_field\_tag "file", :size =\> 75, :class =\> "csv-input" -%\>

**File to Upload**

\<%= submit\_tag -%\>

Here’s my controller code:

class ImportController \< ApplicationController  
def aaccount\_import  
@owner = Owner.find(2)  
n = 0  
unless params[:file].nil?  
fullpath = File.expand\_path(params[:file]) # this doesn’t blow  
up but some books say that a file object is passed and thus File  
methods should apply?  
FasterCSV.foreach("#{RAILS\_ROOT}/convert/#{params[:file]}") do |  
row|

# FasterCSV.foreach(params[:file]) do |row| # cant seem to read

submitting machines full path info?

# FasterCSV.foreach(fullpath) do |row|

```
   c = @owner.accounts.new
   c.code = row[0]
   c.description = row[1]
   if c.save
     n += 1
   else
     n += 1
     d = Badboy.new
     d.message = 'Aaccount Record Failed to convert'
     d.recno = n
     d.save
   end
 end

```

end  
end  
end

You’ll notice that I am able to import this file on MY OWN machine but  
I’m having to HARD CODE the file path before the actual file name that  
is delivered. You can see some ideas I’ve tried to make the  
params[:file] behave like a FILE object and thus reveal its full path.  
I am grateful for any links or suggestions.  
Kathleen

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189/2 "2008-04-29T19:20:23Z")

</div>

is there a destroy method for the file after you’ve used it, maybe?

On Tue, Apr 29, 2008 at 10:03 AM, removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 10, 2008, 10:07am UTC](https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189/3 "2008-06-10T10:07:28Z")

</div>

I don’t know if you already figured this out but I think this is what  
you want:

```
    FasterCSV.parse(params[:file].read) do |row|
     # print "Row =",row,"\n"
     # work with data
    end

```

On Apr 29, 11:03 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 10, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/feed-an-csv-file-up-to-server-for-importing-data-then-throw-away/136189/4 "2008-06-10T14:53:00Z")

</div>

Hi Guys,  
I posted these questions a long time ago and don’t see how they got in  
today’s thread. Here’s an example of how I finally got it to work. You  
question Roger, about how I was going to “throw away” the file once  
downloaded, I have no clue??  
Kathleen

def import\_asscounts  
@accounts = Account.find(:all)  
n = 0  
unless params[:file].nil?  
@account = Account.find(params[:account\_id])  
FasterCSV.new(params[:file]).each do |row|  
if Asscount.find\_by\_account\_id\_and\_code(@account, row[0] )  
@account.asscounts.find(:first, :conditions =\> {:code =\>  
row[0] }).destroy  
end  
c = @account.asscounts.new  
c.code = row[0]  
c.description = row[1]  
if c.save  
n += 1  
else  
n += 1  
d = Badboy.new  
d.account\_id = @account  
d.converttable = ‘Assccount’  
d.keystring = row[0]  
d.keyint = ‘’  
d.message = ‘Asscount Record Failed to convert’  
d.recno = n  
d.save  
end  
end  
flash.now[:notice]= “Asscount Import Successful #{n} new  
records added to data base  
for a total number of  
#{@account.asscounts.size} records”  
end  
end
