<!--:en-->Rails 3 Quicktip: Autoload lib directory including all subdirectories, avoid lazy loading<!--:--><!--:de-->Rails 3 Quicktip: Automatisch lib Ordner und Unterordner laden<!--:--> Lazy cat that represents lazy loading

Rails 3 Quicktip: Autoload lib directory including all subdirectories, avoid lazy loadingRails 3 Quicktip: Automatisch lib Ordner und Unterordner laden

Rails 3 doesn’t autoload files under the lib directory anymore (aka lazy loading). There was quite a discussion about this controversial decision, while lazy loading can be very good and useful, it is also convenient to not have to include every file/folder manually. Fortunately, there is an easy way to enable autoloading again. While most solutions on the Internet only show how to load one directory, here is a solution that autoloads lib including all subdirectories on startup.
Put this in config/application.rb:

lazy cat

Being lazy is good

Update:

See also our blog post, how you can force Rails 3 to reload the lib files: http://hemju.com/2011/02/11/rails-3-quicktip-auto-reload-lib-folders-in-development-mode/

In Rails 3 hat man den Mechanismus für das Laden der lib Dateien von automatisch auf manuell umgestellt (aka lazy loading). Über diese umstrittene Entscheidung gab es bereits einige Diskussionen mit den Entwickler. Lazy Loading hat oftmals Vorteile und seine Berechtigung, aber andererseits ist es auch mühsam jede Datei und Verzeichnis manuell hinzuzufügen. Glücklicherweise gibt es eine einfache Lösung um das alte Verhalten wiederherzustellen. Die meisten Lösungen im Internet zeigen nur wie man ein Verzeichnis automatisch lädt, hier ist eine die automatisch das lib Verzeichnis inklusive aller Unterverzeichnisse lädt.
Einfach folgendes in die Datei config/application.rb eintragen:

lazy cat

Faulheit hat auch gute Seiten

Show Comments Hide Comments
  • http://www.thankful.com thankful

    Good job friend. Thanks for taking the time to share.

  • Pingback: Adoption Curve » Blog Archive » links for 2010-10-18

  • CuriousChap

    This does not reload the classes kept under autoload_paths in dev environment. While googling I figured that one can use require_dependency to make it reloadable However, it still requires one to do it file by file. Is there a better way to make autoload_paths reloadable in dev environment.

  • http://hemju.com Helmut M. Juskewycz

    @CuriousChap You are right it doesn’t reload the classes under lib and I haven’t found a good solution for autoreload.

  • http://ajey.ajey.com ajey

    This worked for me. Thanks a lot.

  • Ajey

    This worked for me. Thanks a lot.

  • http://www.democratandchronicle.com/apps/pbcs.dll/section?category=PluckPersona&U=87636799a3b54305b803b8fc2782220c&plckPersonaPage=BlogViewPost&plckUserId=87636799a3b54305b803b8fc2782220c& karon beach

    I precisely desired to thank you very much once more. I am not sure the things that I would have achieved in the absence of the type of recommendations discussed by you regarding my question. Previously it was a very alarming scenario in my position, but coming across this specialized style you resolved the issue forced me to weep for happiness. Now i’m happier for this support and thus hope you are aware of a powerful job you are always doing training men and women through your websites. I am certain you’ve never got to know all of us.

  • http://www.Blog-infoseite.de/ Blog eintragen

    Blogs are websites that enables the blogger to often add
    content with a selected topic. Blogging are often outlined because the loading
    or adding content on the journal. Remember, there’s a distinction between
    business one thing on a journal and on a daily web site. Some contemplate them
    2 platforms of opposite nature.