Best Practice:: Filling Models With Data

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.

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=>“[email protected]

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.
[email protected]

On Nov 26, 2007 4:33 PM, Will M.
[email protected] 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/

Thanks for the quick reply guys.

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