# Deleting table data from staging table

**URL:** <https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448>\
**Category:** Rails\
**Created:** [August 26, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448 "2009-08-26T21:27:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 26, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448/1 "2009-08-26T21:27:02Z")

</div>

Hello all,  
I’m using Ruby 1.8.6 and Rails 2.3.3.  
I’m new at Rails so bear with me, please.

My application reads in a csv file to a staging (imports) table then  
compares the contents of the table to a main (projects) table. Then  
imports all non-repetitive rows to the main table.  
This application will be used periodically.

I know how to import the csv data to a table.  
How do I delete all data prior to importing new data?

Thank you for all help.

JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 26, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448/2 "2009-08-26T21:54:47Z")

</div>

I have this:  
imports\_controller:

def process\_csv  
# First thing, clear “Imports” table of data  
@import = Import.find(:all)  
@import.destroy

```
file = params[:import][:file]

rowcount = 0

Import.transaction do
  FasterCSV.parse(file,
                  :headers => true,
                  :header_converters => :symbol ) do |row|
    Import.create!(row.to_hash)
    rowcount += 1
  end
end

flash[:notice] = "Successfully added #{rowcount} project(s)."

redirect_to :action => :process_csv

```

## rescue =\> exception # If an exception in thrown, the transaction rolls back and we end up in this # rescue block error = ERB::Util.h(exception.to\_s) # get the error and HTML escape it flash[:error] = “Error adding logs. (#{error}). Please try again.” redirect\_to :action =\> :index end

When I do this I get this error…

“Error adding logs. (undefined method `destroy’ for #Array:0x241d1e0).  
Please try again.”

Any clues?

Thanks,

JohnM

John M. wrote:

> Hello all,  
> I’m using Ruby 1.8.6 and Rails 2.3.3.  
> I’m new at Rails so bear with me, please.
> 
> My application reads in a csv file to a staging (imports) table then  
> compares the contents of the table to a main (projects) table. Then  
> imports all non-repetitive rows to the main table.  
> This application will be used periodically.
> 
> I know how to import the csv data to a table.  
> How do I delete all data prior to importing new data?
> 
> Thank you for all help.
> 
> JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 26, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448/3 "2009-08-26T22:59:00Z")

</div>

2009/8/26 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I know how to import the csv data to a table.  
> How do I delete all data prior to importing new data?

YourModel.delete\_all

Colin

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 26, 2009, 11:04pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448/4 "2009-08-26T23:04:52Z")

</div>

2009/8/26 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have this:  
> imports\_controller:
> 
> def process\_csv  
> Â Â # First thing, clear “Imports” table of data  
> Â Â @import = Import.find(:all)  
> Â Â @import.destroy

Just use Import.delete\_all, provided you do not need any callbacks etc.  
destroy only works on a single record, you are trying to use it on an  
array. You would have to do  
@import.each {|i| i.destroy} or similar

Colin

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 27, 2009, 2:57pm UTC](https://www.ruby-forum.com/t/deleting-table-data-from-staging-table/174448/5 "2009-08-27T14:57:10Z")

</div>

Colin,  
I used the array approach and it worked.

Like this…  
@import = Import.find(:all)  
@import.each { |i| i.destroy }

Thanks for the input.

John

Colin L. wrote:

> 2009/8/26 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I have this:  
> > imports\_controller:
> > 
> > def process\_csv  
> > Â Â # First thing, clear “Imports” table of data  
> > Â Â @import = Import.find(:all)  
> > Â Â @import.destroy
> 
> Just use Import.delete\_all, provided you do not need any callbacks etc.  
> destroy only works on a single record, you are trying to use it on an  
> array. You would have to do  
> @import.each {|i| i.destroy} or similar
> 
> Colin
