# Pre-populate db with yaml outside of testing?

**URL:** <https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542>\
**Category:** Rails\
**Created:** [January 20, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542 "2006-01-20T21:40:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 20, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/1 "2006-01-20T21:40:18Z")

</div>

Hi.

What is the best way to pre-populate your database with records while  
developing, not testing?

For example, I want to:

1. 

> [run this command to populate db]

2. 

> ruby script/server

3. now I can surf to localhost:3000 and my app will already have  
relevant  
data

I’m hoping to use yaml to suck it in.

Is there a way to use the Fixture class to handle this even though this  
is  
outside the typical testing methods?

Thanks,  
Zack

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 20, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/2 "2006-01-20T22:01:28Z")

</div>

That is a great question. I’ll be intersted to see the answers.

I use mysql to do this for me.

Basically, I create a file called data\_load.sql. Rows in it look like  
this-

INSERT INTO table (‘field\_1’, ‘field\_2’) VALUES (‘value 1’, ‘value 2’);

Then every time I blow my development db away, I run -  
mysql -u \<\> -p\<\> database\_name \< data\_load.sql

I think it would be better to use yml because doing what I describe  
above is  
not DRY. I am always setting up the testing yml fixtures, and writting  
insert commands in my data\_load.sql for the same records. To me, that  
seems  
like the worst kind of unDRYness.

matt

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 20, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/3 "2006-01-20T22:58:57Z")

</div>

On Fri, Jan 20, 2006 at 12:00:08PM -0800, Zack C. wrote:

> Is there a way to use the Fixture class to handle this even though this is  
> outside the typical testing methods?

As always, rake is your best friend. Run “rake load\_fixtures” and all  
of  
the data in your fixtures will end up in the current environment’s  
database  
(default: development, but I presume that RAILS\_ENV=production rake  
load\_fixtures would do the obvious thing).

- Matt

–  
A few minutes ago I attempted to give a flying fsck, but the best I  
could do  
was to watch it skitter across the floor.  
– Anthony de Boer, ASR

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 21, 2006, 1:02am UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/4 "2006-01-21T01:02:01Z")

</div>

Matthew P. wrote:

> As always, rake is your best friend. Run “rake load\_fixtures” and all of  
> the data in your fixtures will end up in the current environment’s database  
> (default: development, but I presume that RAILS\_ENV=production rake  
> load\_fixtures would do the obvious thing).

That’s all well and good… but is there an easy way to keep a set of  
fixtures for development that are different from the set you use for  
testing?

thanks,  
j

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 21, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/5 "2006-01-21T18:35:17Z")

</div>

I am surprised this has not come up earlier. How do most people  
pre-populate lookup values in the database? I was hoping there would  
be a structure similar to mocks (mocks/development and mocks/test) in  
the fixtures directory. If you build it like this then you are able to  
satisfy all of the different environments. I definitely don’t want my  
test data to be pushed into my database. Any suggestions would be  
great.

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 21, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/6 "2006-01-21T21:37:16Z")

</div>

Here is a blog post by topfunky that might do what you are looking for:

[http://nubyonrails.topfunky.com/articles/2005/12/27/dump-or-slurp-](http://nubyonrails.topfunky.com/articles/2005/12/27/dump-or-slurp-)  
yaml-reference-data

Cheers-  
-Ezra

On Jan 21, 2006, at 9:32 AM, Carl F. wrote:

> > > As always, rake is your best friend. Run “rake load\_fixtures”  
> > > thanks,  
> > > Rails mailing list  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 23, 2006, 7:02am UTC](https://www.ruby-forum.com/t/pre-populate-db-with-yaml-outside-of-testing/63542/7 "2006-01-23T07:02:13Z")

</div>

Thanks Ezra - I had looked at this as a possibility.

> rake load\_fixtures  
> works great for now.

Zack
