Anyone know how do I run a Ruby script within the Rails framework (like I
was in ./script/console)???
That is, I could do the same things I can do in the Rails console (e.g.
./script/console), however I can write a script file for this. I have
scripting some data fixes in mind.
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
your Rails environment.