Skip to content

Commit 31ad3f1

Browse files
committed
Add a section about autoloading in the Getting Started Guide
1 parent 933284a commit 31ad3f1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

guides/source/getting_started.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ confirming that the `root` route is also mapped to the `index` action of
361361
TIP: To learn more about routing, see [Rails Routing from the Outside In](
362362
routing.html).
363363

364+
Autoloading
365+
-----------
366+
367+
Rails applications **do not** use `require` to load application code.
368+
369+
You may have noticed that `ArticlesController` inherits from `ApplicationController`, but `app/controllers/articles_controller.rb` does not have anything like
370+
371+
```ruby
372+
require "application_controller" # DON'T DO THIS.
373+
```
374+
375+
Application classes and modules are available everywhere, you do not need and **should not** load anything under `app` with `require`. This feature is called _autoloading_, and you can learn more about it in [_Autoloading and Reloading Constants_](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html).
376+
377+
You only need `require` calls for two use cases:
378+
379+
* To load files under the `lib` directory.
380+
* To load gem dependencies that have `require: false` in the `Gemfile`.
381+
364382
MVC and You
365383
-----------
366384

0 commit comments

Comments
 (0)