# Updating model while reading CSV

**URL:** <https://www.ruby-forum.com/t/updating-model-while-reading-csv/178140>\
**Category:** Rails\
**Created:** [October 29, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/updating-model-while-reading-csv/178140 "2009-10-29T17:52:27Z")\
**Posts on this page:** 3\
**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:** [October 29, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/updating-model-while-reading-csv/178140/1 "2009-10-29T17:52:27Z")

</div>

Hello all,  
I was wondering how to update an existing table column when reading in a  
CSV? I’m using FasterCSV and my controller method is this…

- Import Controller -  
def process\_csv

up in this  
# rescue block

```
flash[:error] = "Error adding projects to Import table. (#{error}).

```

Please try again. "

```
redirect_to :action => :new

```

end

As I read the CSV file, I wish to update a column in the model, mainly  
“ProjectType” as the record is being created.

The CSV file does not have this information and there’s no possibility  
that it’ll ever have it.

Thank you for any 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:** [October 30, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/updating-model-while-reading-csv/178140/2 "2009-10-30T14:47:27Z")

</div>

Hey John,

Last week I wrote this code to allow you to easily create models/  
records using FasterCSV… it doesn’t directly answer your question,  
but it might help or at least give you some ideas:  
[http://pastie.org/676681](http://pastie.org/676681)

If it looks this would be useful to you or others I can post it on  
github; I also have a test suite for it. - pat

On Oct 29, 12:52 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [October 30, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/updating-model-while-reading-csv/178140/3 "2009-10-30T14:49:12Z")

</div>

Sorry… forgot to mention the way it works is:

- the first row of the CSV is taken as a list of column names
- the remaining csv rows are data for new models
- if your model contains a column/attribute matching one of the csv  
column names, it will be loaded
- each new record is yielded to a block provided.
