Class: ActiveRecord::Associations::Preloader
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Preloader
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_record/associations/preloader.rb,
lib/active_record/associations/preloader/has_one.rb,
lib/active_record/associations/preloader/has_many.rb,
lib/active_record/associations/preloader/belongs_to.rb,
lib/active_record/associations/preloader/association.rb,
lib/active_record/associations/preloader/has_one_through.rb,
lib/active_record/associations/preloader/has_many_through.rb,
lib/active_record/associations/preloader/through_association.rb,
lib/active_record/associations/preloader/singular_association.rb,
lib/active_record/associations/preloader/collection_association.rb,
lib/active_record/associations/preloader/has_and_belongs_to_many.rb
Overview
Implements the details of eager loading of Active Record associations.
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, Active Record 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.author_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 Active Record is to eager load the associations’ associations 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.
Defined Under Namespace
Modules: ThroughAssociation Classes: Association, BelongsTo, CollectionAssociation, HasAndBelongsToMany, HasMany, HasManyThrough, HasOne, HasOneThrough, SingularAssociation
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#preload_scope ⇒ Object
readonly
Returns the value of attribute preload_scope.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(records, associations, preload_scope = nil) ⇒ Preloader
constructor
Eager loads the named associations for the given Active Record record(s).
- #run ⇒ Object
Constructor Details
#initialize(records, associations, preload_scope = nil) ⇒ Preloader
Eager loads the named associations for the given Active Record 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
.
Parameters
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 all associations records by flattening records
.
associations
specifies one or more associations that you want to preload. It may be:
-
a Symbol or a String which specifies a single association name. For example, specifying
:books
allows this method to preload all books for an Author. -
an Array which specifies multiple association names. This array is processed recursively. For example, specifying
[:avatar, :books]
allows this method to preload an author’s avatar as well as all of his books. -
a Hash which specifies multiple association names, as well as association names for the to-be-preloaded association objects. For example, specifying
{ author: :avatar }
will preload a book’s author, as well as that author’s avatar.
: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 } ]
85 86 87 88 89 |
# File 'lib/active_record/associations/preloader.rb', line 85 def initialize(records, associations, preload_scope = nil) @records = Array.wrap(records).compact.uniq @associations = Array.wrap(associations) @preload_scope = preload_scope || Relation.new(nil, nil) end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
49 50 51 |
# File 'lib/active_record/associations/preloader.rb', line 49 def associations @associations end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
49 50 51 |
# File 'lib/active_record/associations/preloader.rb', line 49 def model @model end |
#preload_scope ⇒ Object (readonly)
Returns the value of attribute preload_scope.
49 50 51 |
# File 'lib/active_record/associations/preloader.rb', line 49 def preload_scope @preload_scope end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
49 50 51 |
# File 'lib/active_record/associations/preloader.rb', line 49 def records @records end |
Instance Method Details
#run ⇒ Object
91 92 93 94 95 |
# File 'lib/active_record/associations/preloader.rb', line 91 def run unless records.empty? associations.each { |association| preload(association) } end end |