# Recording errors

**URL:** <https://discuss.rubyonrails.org/t/recording-errors/44829>\
**Category:** rubyonrails-talk\
**Created:** [August 4, 2009, 11:08pm UTC](https://discuss.rubyonrails.org/t/recording-errors/44829 "2009-08-04T23:08:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discuss.rubyonrails.org/u/DanC)\
**Post date:** [August 4, 2009, 11:08pm UTC](https://discuss.rubyonrails.org/t/recording-errors/44829/1 "2009-08-04T23:08:15Z")

</div>

I am using restful\_authentication in a rails app, but I also wanted to let administrators create news users by piggybacking on the restful\_authentication function.

I decided to allow csv imports for creating members in a two step process similar to this one, [http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/](http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/)

So I import the csv using PaperClip and then process it to create users etc. I also use a rescue in case exceptions are thrown, but I would like to record these errors for display after the entire csv has been imported. I thought about creating a new model, ImportError that belonged to import, so that each csv file could have a list of error messages later on.

But I can't get the recording part to work. Users are created and stored, but the rescue action overrides everything else when errors like duplicate entries come up.

here is my controller.

class ImportsController \< ApplicationController &nbsp;&nbsp;before\_filter :check\_administrator\_role #protect controller from anonymous users

&nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@import = Import.new &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@import = Import.new(params[:import])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @import.save! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'CSV data was successfully imported.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@import) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:error] = 'CSV data import failed.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "new" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def show &nbsp;&nbsp;&nbsp;&nbsp;@import = Import.find(params[:id]) &nbsp;&nbsp;end

&nbsp;&nbsp;def proc\_csv &nbsp;&nbsp;&nbsp;&nbsp;@import = Import.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;lines = parse\_csv\_file(@import.csv.path) &nbsp;&nbsp;&nbsp;&nbsp;#lines.shift #comment this line out if your CSV file doesn't contain a header row &nbsp;&nbsp;&nbsp;&nbsp;if lines.size \> 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@import.processed = lines.size &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lines.each do |line| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_user(line) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@import.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "CSV data processing was successful." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> "show", :id =\> @import.id &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:error] = "CSV data processing failed." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> "show", :id =\> @import.id &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

private

&nbsp;&nbsp;def parse\_csv\_file(path\_to\_csv) &nbsp;&nbsp;&nbsp;&nbsp;lines =

&nbsp;&nbsp;&nbsp;&nbsp;#if not installed run, sudo gem install fastercsv &nbsp;&nbsp;&nbsp;&nbsp;#[http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) &nbsp;&nbsp;&nbsp;&nbsp;require 'fastercsv'

&nbsp;&nbsp;&nbsp;&nbsp;FasterCSV.foreach(path\_to\_csv) do |row| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lines \<\< row &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;lines &nbsp;&nbsp;end

&nbsp;&nbsp;def new\_user(line) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params = Hash.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:user] = Hash.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:user]["name"] = line[0] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:user]["email"] = line[1] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:user]["password"] = random\_password &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:user]["password\_confirmation"] = params[:user] ["password"] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user = User.new(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;member\_role = Role.find\_by\_rolename('member')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @user.save! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.roles \<\< member\_role &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record\_error(line) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::RecordInvalid &nbsp;&nbsp;end

&nbsp;&nbsp;def random\_password(size = 8) &nbsp;&nbsp;&nbsp;&nbsp;chars = (('a'..'z').to\_a + ('0'..'9').to\_a) - %w(i o 0 1 l 0) &nbsp;&nbsp;&nbsp;&nbsp;(1..size).collect{|a| chars[rand(chars.size)] }.join &nbsp;&nbsp;end

&nbsp;&nbsp;def record\_error(line) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params = Hash.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:import\_error] = Hash.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:import\_error]["import\_id"] = @import.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:import\_error]["name"] = line[0] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:import\_error]["email"] = line[1] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:import\_error]["error\_message"] = flash[:error] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@import\_error = User.new(params[:import\_error]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@import\_error.save! &nbsp;&nbsp;end

end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 5, 2009, 7:29am UTC](https://discuss.rubyonrails.org/t/recording-errors/44829/2 "2009-08-05T07:29:28Z")

</div>

This else statement will never execute since if save! fails it raises an exception. Did you want to use save instead ?

Fred

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discuss.rubyonrails.org/u/DanC)\
**Post date:** [August 5, 2009, 8:44am UTC](https://discuss.rubyonrails.org/t/recording-errors/44829/3 "2009-08-05T08:44:13Z")

</div>

Hi Fred,

You are right about the conditional statement being rubbish.

What I really want is to save users that pass the validations and any lines in the csv that fail to get skipped over, but remember the error that was generated.

Without rescue the whole action would fail.

I am just not sure how to record those errors and associate them with this particular import.

Dan

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 5, 2009, 11:13am UTC](https://discuss.rubyonrails.org/t/recording-errors/44829/4 "2009-08-05T11:13:01Z")

</div>

> Hi Fred,
> 
> You are right about the conditional statement being rubbish.
> 
> What I really want is to save users that pass the validations and any lines in the csv that fail to get skipped over, but remember the error that was generated.

Well if you use save rather than save! it won't raise an exception if the record is invalid. If you want the individual errors they'll be in your\_object.errors.

Fred
