This project is archived and is in readonly mode.

#4471 ✓wontfix

Rake Tasks completely screwed up

Reported by Kane | April 25th, 2010 @ 03:25 PM | in 3.0.2

I observed the following behaviour in rails3 beta3.

first of all, it is not possible to modify framework raketasks from lib/tasks/myraketasks.rake as in rails 2.3.5 cause rake tasks from gems are loaded after tasks in lib/tasks. i think this sucks.

therefore to modify tasks i had to do this in the Rakefile.
I found some terrifying behaviour:


Rake::Task['db:schema:load'].enhance do
  puts Rails.env
  puts ActiveRecord::Base.connection.inspect

if i invoke spec:models i get this output:

<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x7f220048a818 @logger=#<ActiveSupport::BufferedLogger:0x7f2200804598 @guard=#<Mutex:0x7f22007ec3a8>, @log=#<File:/home/myusername/myproject/log/development.log>, @auto_flushing=1, @level=0, @buffer={}>, @config={:host=>"localhost", :database=>"myproject_test", :adapter=>"postgresql", :pool=>5, :username=>"postgres", :password=>"postgres"}, @runtime=76.7457485198975, @query_cache_enabled=false, @connection=#<PGconn:0x7f220048a700>, @connection_parameters=["localhost", 5432, nil, nil, "myproject_test", "postgres", "postgres"], @postgresql_version=80403, @async=nil, @active=nil, @query_cache={}>

Rails.env is development?! log is development too?

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper April 25th, 2010 @ 08:04 PM

    • Milestone cleared.
    • State changed from “new” to “open”
    • Assigned user set to “José Valim”
  • José Valim

    José Valim April 25th, 2010 @ 08:12 PM

    Ok, we have two issues here.

    The first one is about rake tasks in gems being loaded after, point taken. This should be fixed.

    The second one is about the output being development needs further investigation. Is rake spec:models setting the environment to test? When? Maybe your rake task is being executed before rspec has the chance to set the environment to test?

    So could you please investigate the second issue and give more information? I will provide a patch for the former.

  • Matt Jones

    Matt Jones April 26th, 2010 @ 01:22 AM

    Some notes on the second point:

    • the enhance method adds functionality that runs after the specified task. See this for more details.

    • there's a certain amount of setup done in development mode before the test stuff can be loaded, for instance to handle loading the schema from dev into test.

    • the connection is in a somewhat mixed state; I'm guessing the myproject_test DB is in the test environment...

  • José Valim

    José Valim April 26th, 2010 @ 07:29 AM

    Thanks Matt. A few more questions: how does the current behavior compares with Rails 2.3? Do you have any fix in mind?

  • Repository

    Repository April 26th, 2010 @ 08:53 AM

    • State changed from “open” to “resolved”

    (from [e461e1bc0ec0a5c365840031309b83143e12955a]) Ensure application rake tasks and generators are loaded after the ones specified in railties/engines/rails. [#4471 state:resolved]

  • Kane

    Kane April 28th, 2010 @ 08:40 PM

    So regarding the second issue:
    I assumed if i run rake db:test:prepare everything would be run in evironment test.
    I didnt looked how deep this goes and how many and which rake tasks are affected.

    So Matt, im not sure if i get you right, but i dont think Rails.env should be dev in any point in the process. Im aware that the process involves loading the dev database, but the env should be definitely test right?

  • José Valim

    José Valim April 28th, 2010 @ 08:42 PM

    • State changed from “resolved” to “open”
  • Nicolas Alejandro Santa

    Nicolas Alejandro Santa April 29th, 2010 @ 11:21 PM

    New Bug:

    running rake db:seed executes the db/seeds.rb script twice times:

        puts "Creating Basic Admin"
        u = User.create(:email => '' , :password => 'admin123', :password_confirmation => 'admin123')
        u.admin = true
        u.confirmation_sent_at = u.confirmed_at =
        u.confirmation_token = nil
        puts "Basic Admin Created"

    Got this output:

    rake db:seed

    Creating Basic Admin
    Basic Admin Created
    Creating Basic Admin
    Basic Admin Created

  • José Valim

    José Valim April 30th, 2010 @ 09:19 AM

    Which rails version? This happens in rails master? It happens with this task only or all others?

  • José Valim

    José Valim April 30th, 2010 @ 12:18 PM

    Nicolas, I just create a new application from Rails source and put the following in my db/seeds.rb

    puts "OMG"

    I invoked "rake db:seed" and it printed "OMG" just once, as expected.

    Kane, regarding the other bug, can you please create a failing test case here:

    Please let me know if you have any doubts.

  • Dan Pickett

    Dan Pickett May 15th, 2010 @ 01:50 AM

    • Tag set to bugmash

    Can some bugmashers reproduce this behavior?

  • Gavin Stark

    Gavin Stark May 15th, 2010 @ 05:48 PM

    Unable to reproduce in 3.0.0beta3

  • Rizwan Reza

    Rizwan Reza May 16th, 2010 @ 02:57 AM

    • Tag cleared.
    • State changed from “open” to “invalid”
  • Kane

    Kane May 26th, 2010 @ 10:06 PM

    Hey José
    i attached the requested failing test case

  • Rizwan Reza

    Rizwan Reza June 15th, 2010 @ 03:12 PM

    • Tag set to rake, tasks
    • State changed from “invalid” to “open”
  • José Valim
  • José Valim

    José Valim June 20th, 2010 @ 11:09 AM

    Could someone please check what is the behavior in Rails 2.3? I'm almost sure that db:test:prepare needs to run in development as Matt pointed out. In other words, you should not be enhancing it to add custom behavior.

  • José Valim
  • José Valim

    José Valim July 21st, 2010 @ 02:30 PM

    • State changed from “open” to “wontfix”
    • Importance changed from “” to “Low”

    Yes, I confirmed this one. When you call rake test, it is actually in development, because you did not pass RAILS_ENV=test. Then it does all setup in development and finally invoke the test command, by shelling out with the proper env configured. There is not much we can do here.

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
  • Jeff Rafter

    Jeff Rafter November 23rd, 2010 @ 08:26 PM

    I was able to reproduce Nicolas' duplication bug in Rails 3.0.3. Entering puts "OMG!" in db/seeds.rb and calling rake db:seed shows the message twice (meaning the rake task is called twice). I spent some time digging around and found that my rake tasks in my application were being loaded twice (duplicating the task). This was caused by the annotate-models gem and associated tasks. Removing those and the behavior was fine. Ultimately instead of

    ENV['position'] ||= 'after' require 'annotate_models/tasks'

    I now have

    ENV['position'] ||= 'after' desc "Add schema information (as comments) to model and fixture files" task :annotate_models => :environment do

    require 'annotate_models'
    options[:position_in_class] = ENV['position_in_class'] || ENV['position']
    options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position']  


    desc "Remove schema information from model and fixture files" task :remove_annotation => :environment do

    require 'annotate_models'


    And everything works as expected.


Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>



Referenced by