Another approach is Rake. I don’t do it in the model un less it’s really part of the app.
namespace :setup do
desc “Set up initial roles in the database”
task :roles => :environment do
# Drop any roles that might already be in the table
puts "Removing any existing roles"
Role.all.each do |r|
r.destroy # forcing callbacks to cascade delete
puts "Creating roles..."
Role.create :name => "User"
Role.create :name => "Admin"
# some other Ruby / Rails related code
The key here is that => :environment in the test declaration that brings in your Rails environment.