This project is archived and is in readonly mode.

#5718 ✓stale
stefanpenner

Models fail to autoload in Rake files if config.threadsafe!

Reported by stefanpenner | September 28th, 2010 @ 05:33 AM

if config.threadsafe!, rake tasks in lib/tasks fail to autoload models.

Either eager loading OR autoloading would be great, currently we have neither.

Comments and changes to this ticket

  • David Trasbo

    David Trasbo October 9th, 2010 @ 03:58 PM

    • State changed from “new” to “incomplete”
    • Importance changed from “” to “Low”

    Confirmed on edge Rails:

    $ script/rails g model foo
    ...
    $ rake db:migrate
    (in /Users/dtrasbo/Documents/Code/rails/railties/tmp/ticket_5718)
    ...
    

    lib/tasks/test.rake:

    task :test do
      Foo
    end
    
    $ rake hello
    (in /Users/dtrasbo/Documents/Code/rails/railties/tmp/ticket_5718)
    rake aborted!
    uninitialized constant Object::Foo
    
    (See full trace by running task with --trace)
    

    Please provide a patch (https://rails.lighthouseapp.com/projects/8994/sending-patches) with a fix and/or a failing test.

  • José Valim

    José Valim November 7th, 2010 @ 04:54 PM

    • State changed from “incomplete” to “invalid”

    You need to have a dependency on the environment task:

    @@@ruby task :test => :environment do
    Foo end

    
    This will load the environment and eager load your models.
    
  • stefanpenner

    stefanpenner November 7th, 2010 @ 05:26 PM

    José,

    unfortunately with config.threadsafe! enabled loading the environment dependency does not eager load models.

        $ bundle exec rails g model foo 
        ...
        rake db:migrate
        ...
    
        task :foo => :environment do
          Foo
        end
    
        $ bundle exec rake foo 
        rake aborted!
        uninitialized constant Foo
        /Users/stefan/tmp/threadsafe/Rakefile:9
    
  • José Valim

    José Valim November 7th, 2010 @ 06:44 PM

    • State changed from “invalid” to “open”
    • Assigned user set to “José Valim”

    Oh snap! Reopening this ticket then.

  • Hudson

    Hudson January 18th, 2011 @ 06:27 PM

    I have noticed the same behaviour with migrations. If I want to use models from my app in migrations, I need to explicitly require the model and any of it's dependencies at the top of the migration. This is only necessary for environments where config.threadsafe! is set.

  • Roger Leite

    Roger Leite January 28th, 2011 @ 05:12 PM

    I know this strange, but, Isn't an expected behaviour ?

    Copied from "http://guides.rubyonrails.org/configuring.html#configuring-action-c...:

    config.action_controller.allow_concurrency should be set to true to allow concurrent (threadsafe) action processing. Set to false by default. You probably don’t want to call this one directly, though, because a series of other adjustments need to be made for threadsafe mode to work properly. Instead, you should simply call config.threadsafe! inside your production.rb file, which makes all the necessary adjustments.

    WARNING:

    Threadsafe operation is incompatible with the normal workings of development mode Rails. In particular, automatic dependency loading and class reloading are automatically disabled when you call config.threadsafe!.

  • Robin Salkeld

    Robin Salkeld February 5th, 2011 @ 01:10 AM

    The problem is actually that eager loading of application classes is disabled for rake tasks. From initializer.rb:

    def load_application_classes

    return if $rails_rake_task
    
    ...
    

    end

    I think this is intended to avoid catch-22s where you have a model class that borks if its database table doesn't exist, but you can't run the migration to create it because loading the Rails environment loads the class.

    The issue is that the threadsafe! option still disables dependencies loading, so you can't refer to any of those classes implicitly if you actually want to.

    I think the fix (perhaps instead of the existing guard in load_application_classes?) might be to ignore the threadsafe! and/or cache_classes options in rake tasks. After all, you're never accepting any controller requests within a rake runtime, so why would you care anyway? :)

  • rails

    rails May 7th, 2011 @ 01:00 AM

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails May 7th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”
  • klkk

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages