Ruby-on-rails – Confused AutoLoad_paths and Eager_Load_paths in Rails 4

I read a post about rails load_paths, here is the link.

However, I am still confused about the difference between autoload_paths and eager_load_paths :

I tested them in a newly created Rails 4 project. They seem to behave in the same way, i.e. automatically reload in development mode but in production mode.

The author of this linked article. This is an attempt to eliminate confusion, starting with @fkreusch’s answer.

In Ruby, you must require every .rb file to run its code. However, please note that in Rails you never specifically require any models, controllers or other files in app/dir. Why is this? That’s because in Rails app/* is in autoload_paths. This means that when you run a rails app in development (e.g. via rails console)-ruby​​ does not actually need any models and controllers. Rails uses Ruby’s special magic function actually waits until the code mentions a constant, such as Book, and then it runs. It needs’book’, which is found in autoload_paths. This allows you to start the console and server faster during development, Because no action is required to start it, only when the code actually needs it.

Now, this behavior is good for local development, but what about production? Imagine that in production, your server will perform the same type of magical constant loading (auto loading). It’s really not the end of the world, you start your server in production, and people start browsing your page slightly slower because of some Files need to be loaded automatically. Yes, these initial requests are slower, and the server “warms up”, but it’s not that bad. Other than that, this is not the end of the story.

If You are running on ruby ​​1.9.x (if I remember correctly), then automatically requiring such files is not thread-safe. Therefore, if you use a server like puma, you will run into problems. Even if you don’t use much Thread server, you may still be better at “proactively” requiring the entire application at startup. This means that in production, you want every model, every controller, etc., to be fully required when starting the application, and you don’t mind Longer boot time. This is called eager loading. All ruby ​​files are eagerly loaded, get it? However, if your rails application does not have a single require statement, how can you do this? This is where eager_load_paths comes in. No matter what you enter, all files in all directories under these paths are required when production starts. Hope this clears it.

It is important to note that eager_load_paths is in development The environment is not active, so you don’t need to put them eagerly during the development process, and only need them in production.

It is also important to note that you only need to put something into autoload_paths. Will not let it rush to load in production. Unfortunately. You must also explicitly put it in eager_load_paths.

Another interesting quirk is that in every rails application, the app/under All directories are automatically in autoload_paths and eager_load_paths, which means no further operations are required to add directories there.

I read a post about rails load_paths, here Yes link.

However, I am still confused about the difference between autoload_paths and eager_load_paths:

I tested them in a newly created Rails 4 project. They seem to be Run in the same way, i.e. automatically reload in development mode but in production mode.

The author of this linked article. This is an attempt to eliminate confusion , Starting from @fkreusch’s answer.

In Ruby, you must require every .rb file to run its code. However, please note that in Rails you never specifically require app/dir Any models, controllers or other files in the file. Why is this? That’s because in Rails app/* is in autoload_paths. This means that when you run a rails app in development (e.g. via rails console)-ruby​​ does not actually need any models and controllers. Rails uses Ruby’s special magic function actually waits until the code mentions a constant, such as Book, and then it runs. It needs’book’, which is found in autoload_paths. This allows you to start the console and server faster during development, Because no action is required to start it, only when the code actually needs it.

Now, this behavior is good for local development, but what about production? Imagine that in production, your server will perform the same type of magical constant loading (auto-loading). It really is not the end of the world, you start your server in production, and people start browsing your page slightly slower because of some Files need to be loaded automatically. Yes, these initial requests are slower, and the server “warms up”, but it’s not that bad. Other than that, this is not the end of the story.

If You are running on ruby ​​1.9.x (if I remember correctly), then automatically requiring such files is not thread-safe. Therefore, if you use a server like puma, you will run into problems. Even if you don’t use much Thread server, you may still better need the entire application “actively” at startup. This means that in production, you want every model, every controller, etc., to be fully required when you start the application, and you don’t mind Longer boot time. This is called eager loading. All ruby ​​files are eagerly loaded, get it? However, if your rails application does not have a single require statement, how can you do this? This is where eager_load_paths comes in. No matter what you enter, all files in all directories under these paths are required when production starts. Hope this clears it.

It is important to note that eager_load_paths is in development The environment is not active, so there is no eager need to put them in the development process, only in production.

It is also important to note that just put something into autoload_paths. Will not let it rush to load in production. Unfortunately. You must also explicitly put it in eager_load_paths.

Another interesting quirk is that in every rails application, the app/ All directories are automatically in autoload_paths and eager_load_paths, which means that no further operations are required to add directories there.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3326 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.