# Best approach for importing data from a file

**URL:** <https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663>\
**Category:** Rails\
**Created:** [October 24, 2008, 4:24am UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663 "2008-10-24T04:24:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [October 24, 2008, 4:24am UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/1 "2008-10-24T04:24:20Z")

</div>

Hi All,

I’ve got a basic Rails app started. One wrinkle is that the user  
wants ideally to be able to browse the files system (a la Windows  
Explorer) to select a file and initiate the importation of data from  
the file.

All files selected will be of a predetermined fixed format, e.g. CSV  
with Name, Address, etc.

It seems to me that the browser is isolated from the file system,  
albeit Rails’ internals know how to get to it. Is there a way to  
implement the above-described functionality in a Rails app? Can YAML  
be used for such functionality?

The only approach I see right now is a Ruby database-updater using  
perhaps Active Record with ar-extensions or Active Scaffold.

Any guidance would be most appreciated.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [October 24, 2008, 6:15am UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/2 "2008-10-24T06:15:57Z")

</div>

A Google search for

rails import csv

returns several promising results.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [October 24, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/3 "2008-10-24T13:45:23Z")

</div>

Hi All,

I’ve thought more about this and think I know how to proceed with a  
Rails-integrated approach.

Thanks anyway if you’ve looked at the original post.

Best wishes,  
Richard

On Oct 23, 10:23 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [October 24, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/4 "2008-10-24T13:47:45Z")

</div>

Thanks, Craig. That search suggestion does help.

Best wishes,  
Richard

On Oct 24, 12:15 am, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [October 27, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/5 "2008-10-27T18:57:02Z")

</div>

Take a look at attachment\_fu.

On Oct 23, 4:23 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [November 13, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/6 "2008-11-13T16:12:23Z")

</div>

Hi Rick,

I’ve been busy, so I haven’t checked for responses to my posts for a  
week or so. Sorry for the delay in this response.

Thanks for your response. I just ordered Advanced Rails Recipes and  
looked at the attachment\_fu.rb from the book. I’m too weak in Rails  
to see how that’s useful for my project.

I think I’ve concocted a solution to my main concern: how to access a  
CVS file from the file-system from within a Rails app. I now believe  
I can:

1. get the filenames housed in a specific directory from within a  
controller using normal Ruby programming.
2. display those filenames as a list in a view and allow the user to  
select any one of then
3. open the selected file and read its records, one by one
4. extract the fields from each cvs-formatted record and update the  
database with the contents of those fields.

Does that sound sensible to you?

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [November 24, 2008, 11:59am UTC](https://www.ruby-forum.com/t/best-approach-for-importing-data-from-a-file/151663/7 "2008-11-24T11:59:21Z")

</div>

Hey Richard,

Sorry it’s been so long, I seem to have dropped this thread.

1. My thought about attachment\_fu is that it allows you to pull files  
that are local to your “rails client” machine and bring them into the  
rails application space. One way it’s used is to enable rails users  
to post their own images onto a site for sharing with the community.  
There is a browser included that works much like the usual file  
browser to aid with locating / selecting file for inclusion.

I’m not sure where your csv files live right now but if they’re not  
somewhere below RAILS\_ROOT + “public” then you’ll need to figure out  
how to get them there. Attachment\_fu can help here if the files are  
user generated - field data and the like - that the user will upload  
into the system.

1. The next thing is moving the data into your Model(s). I’m not sure  
how comfortable you are with the quality of the data but I guarantee  
that, no matter what approach you take, you’ll need to constantly work  
on data validation.

You’ll probably do best if you start with one model for each file  
type. That way you can use the model.rb file to validate, validate,  
validate,… the data on it’s way to the database.

1. Of course if it’s your own data and you know it’s always complete  
and correct, you can take the migration file approach. It’s a little  
more raw than what is generally considered socially acceptable in the  
RoR community. The real drawback is then specific database methods  
leak in past the abstraction boundary. Notice the code for postgresql  
differs from that for mysql - and we haven’t even looked at sqlite3 or  
db2 or …😛

class CreateStateAbbrevs \< ActiveRecord::Migration  
def self.up  
create\_table :state\_abbrevs do |t|  
t.string :name  
t.string :abbrev  
end  
csv\_file = “#{RAILS\_ROOT}/db/migrate/abbr\_state.csv”  
fields = ‘(name, abbrev)’

```
# PostgreSQL load from file...
execute "COPY state_abbrevs #{fields} FROM '#{csv_file}' WITH CSV"

# MySQL load from file...
# execute "LOAD DATA INFILE '#{csv_file}' INTO TABLE state_abbrevs

```

FIELDS " +  
# “TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY “””" " +  
# "LINES TERMINATED BY ‘\n’ " + fields

end

def self.down  
drop\_table :state\_abbrevs  
end  
end

So many buttons and so little time,  
Rick

On Nov 13, 5:11 am, RichardOnRails
