rake + eval

I have a rake task that contains an eval call.

namespace :db do   namespace :load do     desc 'Loads factories into db'     task :factories => :environment do       ActiveRecord::Base.establish_connection( ENV[ 'RAILS_ENV' ] )       FACTORIES.each do |f|         puts "Loading factory: #{ f }"         data = IO.read( "#{ RAILS_ROOT }/test/factories/#{ f }.rb" )         eval data       end     end   end end

My problem is the eval executes when loading script/console or executing `rake -T`, it fires even though I'm not calling it.

What's a better way?

I'm almost certain that neither of these issues is causing the problem you're seeing, but who knows...

- why are you calling establish_connection directly? The :environment task should be doing it for you...

- rather than reading and eval'ing, why not just use load()?

--Matt Jones

Thanks. Following your suggestions I now have:

namespace :db do   namespace :load do     desc 'Loads factories into db'     task :factories => :environment do       FACTORIES.each do |f|         load( "#{ RAILS_ROOT }/test/factories/#{ f }.rb" )       end     end   end end

Works fine but my problem still exists. When I do `rake -T` I see in the logs my code from test/factories/* is being ran, data is being inserted.

Something else I noticed.. if I put a debugger call inside the task block it drops me to a prompt twice, not just once as expected.

So at this point I've tried eval, load, and system, and all are being executed without the rake task being called.

Anyone?

I have to admit I'm pretty stumped. If you make another rake task, does it get loaded as well? Maybe a dummy task like:

desc "Does nothing" task :dummy_task => :environment do   puts "Blargh!" end

If that gets run, there's something really weird going on with your Rake. If not, then something about the db:load task is weird...

--Matt Jones