class ActiveRecord::Associations::Preloader
def initialize(records:, associations:, scope: nil, available_records: [], associate_by_default: true)
queries by reusing in-memory objects. The optimization is only applied
associations before querying the database. This can save database
will try to use the objects in this array to preload the requested
+available_records+ is an array of ActiveRecord::Base. The Preloader
[ :books, { author: :avatar } ]
{ author: :avatar }
[ :books, :author ]
:books
ActiveRecord::QueryMethods. So +associations+ could look like this:
+:associations+ has the same format as the +:include+ method in
book's author, as well as that author's avatar.
example, specifying { author: :avatar } will preload a
association names for the to-be-preloaded association objects. For
- a Hash which specifies multiple association names, as well as
books.
allows this method to preload an author's avatar as well as all of his
is processed recursively. For example, specifying [:avatar, :books]
- an Array which specifies multiple association names. This array
for an Author.
example, specifying +:books+ allows this method to preload all books
- a Symbol or a String which specifies a single association name. For
preload. It may be:
+associations+ specifies one or more associations that you want to
flattening +records+.
+preload_associations+ will preload all associations records by
i.e. +records+ itself may also contain arrays of records. In any case,
+records+ is an array of ActiveRecord::Base. This array needs not be flat,
== Parameters
names +:author+ and +:buyers+.
belongs_to :author, has_many :buyers has association
to an association creation method. For example, a model that specifies
In this description, 'association name' shall refer to the name passed
Eager loads the named associations for the given Active Record record(s).
def initialize(records:, associations:, scope: nil, available_records: [], associate_by_default: true) @records = records @associations = associations @scope = scope @available_records = available_records || [] @associate_by_default = associate_by_default @tree = Branch.new( parent: nil, association: nil, children: @associations, associate_by_default: @associate_by_default, scope: @scope ) @tree.preloaded_records = @records end