# Best Practice:: Filling Models With Data

**URL:** <https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335>\
**Category:** Rails\
**Created:** [November 26, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335 "2007-11-26T23:33:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [November 26, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335/1 "2007-11-26T23:33:04Z")

</div>

What is the best way (Rails 2.0) for auto populating your models with  
data.

For example if I have a category table with several types that are  
pre-defined. What is the best method of auto populating the database  
during deployments?

Thanks.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [November 26, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335/2 "2007-11-26T23:41:08Z")

</div>

Use a Rake task.

In Lib/tasks, make this file

import.rake

In that file, put this code:

namespace :db do

desc “Boostrap my database with default data”  
task :import =\> :environment do  
User.create :login =\> “admin”, :password=\>“admin”,  
:password\_confirmation=\>“admin”, email=\>“removed\_email\_address@domain.invalid”

# other cool ruby code goes here

end

end

Then, as part of your deployment,

rake RAILS\_ENV=“production” db:import

(make that part of your deployment recipe if you wish!)

Hope that helps!

On Nov 26, 2007 4:33 PM, Will M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [November 26, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335/3 "2007-11-26T23:43:28Z")

</div>

On Nov 26, 2007 4:33 PM, Will M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best way (Rails 2.0) for auto populating your models with  
> data.
> 
> For example if I have a category table with several types that are  
> pre-defined. What is the best method of auto populating the database  
> during deployments?

Fixtures. RAILS\_ENV=production rake db:fixtures:load

And then sometimes I just create data in the migrations, especially if  
I don’t have any fixtures up to that point.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [November 26, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335/4 "2007-11-26T23:59:42Z")

</div>

Thanks for the quick reply guys.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [November 27, 2007, 2:29am UTC](https://www.ruby-forum.com/t/best-practice-filling-models-with-data/121335/5 "2007-11-27T02:29:32Z")

</div>

you should reserve fixtures for tests. Rake is a much better tool for  
production db setup.
