Forum: Ruby on Rails rake + eval

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-06-02 20:20
(Received via mailing list)
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 Donald
http://destiney.com/
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-06-03 16:07
(Received via mailing list)
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
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-06-03 16:21
(Received via mailing list)
On Wed, Jun 3, 2009 at 9:05 AM, Matt Jones <al2o3cr@gmail.com> 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 Donald
http://destiney.com/
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-06-04 15:14
(Received via mailing list)
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
This topic is locked and can not be replied to.