# \[ANN\] CSV Importer for ActiveRecord

**URL:** <https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330>\
**Category:** Rails\
**Created:** [July 25, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330 "2007-07-25T12:30:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [July 25, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330/1 "2007-07-25T12:30:10Z")

</div>

Hey all,

Just thought I’d let you all know that I’ve posted a CSV importer  
plugin for Rails that adds an import method to ActiveRecord:  
[http://www.stephenbartholomew.co.uk/2007/7/20/csv-importer-for-rails](http://www.stephenbartholomew.co.uk/2007/7/20/csv-importer-for-rails).

It’s fairly basic at the moment but I’ve been using it a fair bit.  
Check it out and let me know what you think.

Cheers,

Steve

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 2, 2007, 1:43am UTC](https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330/2 "2007-11-02T01:43:09Z")

</div>

Stephen B. wrote:

> Hey all,
> 
> Just thought I’d let you all know that I’ve posted a CSV importer  
> plugin for Rails that adds an import method to ActiveRecord:  
> [http://www.stephenbartholomew.co.uk/2007/7/20/csv-importer-for-rails](http://www.stephenbartholomew.co.uk/2007/7/20/csv-importer-for-rails).
> 
> It’s fairly basic at the moment but I’ve been using it a fair bit.  
> Check it out and let me know what you think.
> 
> Cheers,
> 
> Steve  
> Well, I need to import a CSV file, but I may be too new to Rails to use  
> it.

I downloaded it to the vendor/plugins directory, but now don’t know what  
to do with it. The code on your site appears to be OK, but I don’t know  
where to place the pieces.

I have to manipulate the imported data considerably before writing it to  
the database.

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 3, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330/3 "2007-11-03T19:47:33Z")

</div>

Alan,

I dont know if it may be of help, but I had a similar situation, where  
I have to transfer data in csv (actually newline and tab delimited  
text) into a database model. The approach I have taken, which has  
served me well was to create a model just for the imported data.  
Creating a method for each field. The structure of the data was such  
that for me it was best to use split functions rather than try to  
parse the data.

Now I can take a row from the incoming file, and call new on the csv  
model. This then splits the data and holds it in a partial form. eg.  
I have to initially split multiple records that are separated by \n–  
\n etc. I can then decide to hold partially in the best form for the  
structure I am dealing with. Then I can pull out the fields as I need  
them.

The data contains Customer, Order and Lineitem information. So  
internally in the imported data model, I hold the customer fields as  
an array, the Order fields as an array, and the Lineitems as an array  
of Class LineItem ( a Class within the imported order model.

Also then I have included in the model, methods for order\_fields,  
customer\_fields and line\_item fields. These return a hash of the  
fields which can be used to create a new instance of the database  
model.

By using a model for the csv data, it makes it easy to test and  
maintainn being easier to adapt if the csv format changes. And means  
the controller part of the code is kept clean as follows:

@data\_records.each do |row|  
lines=ImportedOrder.new(row)

```
  # create the order and line_items
  @order=Order.new(line.order_fields)
  @order.customer=Customer.new(line.customer_fields)
  line.line_items.each do |item|
    @line_item=LineItem.new(item.fields)
    @order.line_items<<@line_item
  end

   @order.save

```

end

I have stripped out some extra stuff to test for different types of  
order, count updated records and provide logging etc. I have also  
changed the naming to make more general, so I hope the above is  
correct. Anyway, the point I am trying to make is that if you  
implement the right methods at the model level, it should be fairly  
simple to create the database records based on that model.

hth  
Tonypm

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 3, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/ann-csv-importer-for-activerecord/110330/4 "2007-11-03T23:32:18Z")

</div>

Tonypm,

I really appreciate this! I’m not certain that I understand all that  
you said, but I think I can figure it out. I have Agile Web D.  
on Rails and several Ruby books, including, of course, the Pickaxe.

The program I’m writing is one that I already wrote in PHP, but after a  
year away from it, I find it very difficult to maintain. It puts the  
input data into a bunch of Celko trees.
