# Import of YAML files during rake?

**URL:** <https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225>\
**Category:** Rails\
**Created:** [October 18, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225 "2008-10-18T14:20:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225/1 "2008-10-18T14:20:36Z")

</div>

I want to import yaml files in to my project.

I have tried with fixtures, but it deletes the current data of the  
table.  
I need a way to import yaml filen in to a exsistant table.

regards  
svend

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225/2 "2008-10-18T16:03:44Z")

</div>

On Oct 18, 11:19 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I want to import yaml files in to my project.
> 
> I have tried with fixtures, but it deletes the current data of the  
> table.  
> I need a way to import yaml filen in to a exsistant table.

If you want to load yaml data into your development database, for  
instance, you can add migrations to do this.  
In db/migrate you should have the migration files for your tables  
which you created when you generated your models. Let’s suppose you  
are loading to the ‘users’ table then you might run:  
% script/generate migration LoadUserData

## Migration file might look like this:

require ‘active\_record/fixtures’

class LoadUserData \< ActiveRecord::Migration  
def self.up  
down  
directory = File.join( File.dirname( **FILE** ) , ‘data’ )  
Fixtures.create\_fixtures(directory, ‘users’)  
end

## def self.down User.delete\_all end end

where;  
‘data’ is db/migrate/data directory; and  
‘users’ is the db/migrate/data/users.yml file - which will load into  
‘users’ table for your ‘User’ model.

Then run your migrations:  
% rake db:migrate VERSION=0  
% rake db:migrate

I think this technique is in the Rails book.

–  
Daniel B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225/3 "2008-10-18T21:14:05Z")

</div>

Thanx, but using fixtures\_create removes the current contense of the  
database.  
This is not for devel, this is in the production enviroment.

regards  
svend

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2008, 2:20am UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225/4 "2008-10-19T02:20:21Z")

</div>

On Oct 19, 6:13 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanx, but using fixtures\_create removes the current contense of the  
> database.  
> This is not for devel, this is in the production enviroment.
> 
> regards  
> svend

I’ve only used the technique I mentioned to populate the table in its  
totality. If you’re appending data to a table, you could perhaps  
create a loading table as a copy of the table you’re loading to (sql:  
create as select \* from

 where 1=0; (not sure  
if it works on every db) / or do it the migration way). Use the  
fixture technique discussed to load it with data from your yaml file.  
Then run an insert sql statement: ‘insert into 

 select \* from  
’.

–  
Daniel B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/import-of-yaml-files-during-rake/151225/5 "2008-10-19T18:42:21Z")

</div>

I hate it when people think out of the box, and comes up with a  
perfect simple solution.  
Perfect, thanx for the help.

regards  
svend
