Rails force models to eager load

rails eager load association
rails eager load paths
rails includes
rails eager load children
rails eager load lib
rails fully_load
use eager loading detected
rails preload scope

I would like to be able to load an entire app so that I may find the descendants of a given class.

For example given I have the following class defined:

# app/models/foo_class.rb
class FooClass < MySpecialBaseClass
  # pasta code
end

It won't be found with:

irb> ObjectSpace.each_object.select { |obj| obj.is_a?(Class) && obj <= MySpecialBaseClass }
=> []

Until I call the const:

irb> FooClass

Then it is returned:

irb> ObjectSpace.each_object.select { |obj| obj.is_a?(Class) && obj <= MySpecialBaseClass } 

=> [FooClass]

How would I go about doing this?


After a great deal of trial and error, I recently learned that Jason Waldrip's answer is somewhat incomplete. As of Rails 5, Rails.application.eager_load! will indeed load all of the directories specified in config/application.rb. But it doesn't match Rails' actual behavior in production. To do that, one must instead mirror what Rails does:

Rails.configuration.eager_load_namespaces.each(&:eager_load!)

The salient difference between the approaches is that OPs answer won't eager load the files within app directories of Engines that live in the gems or vendor folders. Whereas Rails itself will identify where subclasses of Engine exist and will see to it that the appropriate app subdirectories are eager-loaded.

Behind the scenes

Rails 5 adds eager load directories in railties-5.0.2/lib/rails/engine/configuration.rb:39, where it runs

      paths.add "app",                 eager_load: true, glob: "{*,*/concerns}"
      paths.add "app/assets",          glob: "*"
      paths.add "app/controllers",     eager_load: true
      paths.add "app/channels",        eager_load: true, glob: "**/*_channel.rb"
      paths.add "app/helpers",         eager_load: true
      paths.add "app/models",          eager_load: true
      paths.add "app/mailers",         eager_load: true

These directories are not currently included in a default Rails.application.eager_load!

Understanding Rails Eager-Loading - DEV, My aim was to write a Rails eager-loading guide that's heavy on motivation This post assumes that you understand model associations, are  There are 3 ways to do eager loading in Rails: #includes; #preload; #eager_load; #includes delegates the job to #preload or #eager_load depending on the presence or absence of condition related to one of the preloaded table. #preload is using separate DB queries to get the data. #eager_load is using one big query with LEFT JOIN for each eager loaded table.


Well, after some digging, it actually is really simple. Just need to run the following.

Rails.application.eager_load!

Automatic Eager Loading in Rails, This post explores Rails automatically handle eager loads. the issues with eager loading by considering the following models for a blogging  To rescue us from such situations, Rails offers us a feature called eager loading. Eager loading lets you preload the associated data (authors) for all the posts from the database, improves the overall performance by reducing the number of queries, and provides you with the data that you want to display in your views, but the only catch here is which one to use.


From Configuring Rails Applications

  1. config.eager_load when true Eager loads all registered config.eager_load_namespaces. This includes your application, engines, Rails frameworks and any other registered namespace.
  2. config.eager_load_namespaces registers namespaces that are eager loaded when config.eager_load is true. All namespaces in the list must respond to the eager_load! method.
  3. config.eager_load_paths accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the app directory of the application.

EDIT:

To manually load you should be able to do something like:

matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
  require_dependency file.sub(matcher, '\1')
end

10 Tips for Eager Loading to Avoid n+1 Queries in Rails, preload: Initiates two queries, the first to fetch the primary model and the second to fetch associated models. eager_load: Does a left outer join  Most Rails developers quickly learn that minimizing the number of database queries by properly specifying eager loads is critical for good application performance. Unfortunately specifying eager loads is error prone and can cause encapsulation problems. In this post we'll explore having Rails automatically handle eager loads.


With Rails 6, Zetiwerk became the default code loader.

Try this for eager loading:

Zeitwerk::Loader.eager_load_all

Improving Database performance and overcoming common N+1 , The performance of our Rails application depends on many variables, and The general way to solve N + 1 is to use includes to eager load the associations. preload initiates two queries, the first to fetch the primary model and the Record unless we force it to use the other strategy used by eager_load . config.eager_load_paths accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the app directory of the application. config.enable_dependency_loading: when true, enables autoloading, even if the application is eager loaded and config.cache_classes is set as true.


Improving the Performance of Your Rails App With Eager Loading, This is the very basic association/relation between these two models that So by default in Ruby on Rails, the ORM has lazy loading enabled,  13.1 Eager Loading Multiple Associations. Active Record lets you eager load any number of associations with a single Model.find call by using an array, hash, or a nested hash of array/hash with the includes method. 13.1.1 Array of Multiple Associations


Rake ignores eager loading Rails config · Codegram, I recently hit an unexpected behavior: Rake was not eager-loading classes, even though my Rails config said so. Here's why! Eager loading puts everything in memory ready to serve requests right away, and it is also CoW -friendly. Eager loading is controlled by the flag config.eager_load, which is enabled by default in production mode. The order in which files are eager loaded is undefined.


→ Eager Loading (preloading), There are 3 ways to do Eager Loading in Rails: #includes, #preload, #​eager_load. Want to know more about it? #CLICK HERE to visit Arkency  Contribute to rails/rails development by creating an account on GitHub. Previously, the eager load behavior was mostly coupled to config.cache_classes, however this was suboptimal since in some environments a developer may want to cache classes but not necessarily load