# Rake + eval

**URL:** <https://www.ruby-forum.com/t/rake-eval/169359>\
**Category:** Rails\
**Created:** [June 2, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/rake-eval/169359 "2009-06-02T20:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [June 2, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/rake-eval/169359/1 "2009-06-02T20:20:43Z")

</div>

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?

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

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [June 3, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/rake-eval/169359/2 "2009-06-03T16:07:33Z")

</div>

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 J.

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [June 3, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/rake-eval/169359/3 "2009-06-03T16:21:44Z")

</div>

On Wed, Jun 3, 2009 at 9:05 AM, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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()?

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?

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

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [June 4, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/rake-eval/169359/4 "2009-06-04T15:14:45Z")

</div>

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 J.
