Module | ActiveRecord::AssociationPreload::ClassMethods |
In: |
lib/active_record/association_preload.rb
|
Implements the details of eager loading of ActiveRecord associations. Application developers should not use this module directly.
ActiveRecord::Base is extended with this module. The source code in ActiveRecord::Base references methods defined in this module.
Note that ‘eager loading’ and ‘preloading’ are actually the same thing. However, there are two different eager loading strategies.
The first one is by using table joins. This was only strategy available prior to Rails 2.1. Suppose that you have an Author model with columns ‘name’ and ‘age’, and a Book model with columns ‘name’ and ‘sales’. Using this strategy, ActiveRecord would try to retrieve all data for an author and all of its books via a single query:
SELECT * FROM authors LEFT OUTER JOIN books ON authors.id = books.id WHERE authors.name = 'Ken Akamatsu'
However, this could result in many rows that contain redundant data. After having received the first row, we already have enough data to instantiate the Author object. In all subsequent rows, only the data for the joined ‘books’ table is useful; the joined ‘authors’ data is just redundant, and processing this redundant data takes memory and CPU time. The problem quickly becomes worse and worse as the level of eager loading increases (i.e. if ActiveRecord is to eager load the associations’ assocations as well).
The second strategy is to use multiple database queries, one for each level of association. Since Rails 2.1, this is the default strategy. In situations where a table join is necessary (e.g. when the +:conditions+ option references an association‘s column), it will fallback to the table join strategy.
See also ActiveRecord::Associations::ClassMethods, which explains eager loading in a more high-level (application developer-friendly) manner.
Eager loads the named associations for the given ActiveRecord record(s).
In this description, ‘association name’ shall refer to the name passed to an association creation method. For example, a model that specifies belongs_to :author, has_many :buyers has association names +:author+ and +:buyers+.
records is an array of ActiveRecord::Base. This array needs not be flat, i.e. records itself may also contain arrays of records. In any case, preload_associations will preload the associations all records by flattening records.
associations specifies one or more associations that you want to preload. It may be:
+:associations+ has the same format as the +:include+ option for ActiveRecord::Base.find. So associations could look like this:
:books [ :books, :author ] { :author => :avatar } [ :books, { :author => :avatar } ]
preload_options contains options that will be passed to ActiveRecord#find (which is called under the hood for preloading records). But it is passed only one level deep in the associations argument, i.e. it‘s not passed to the child associations when associations is a Hash.