По второму вопросу вы можете попробовать погрузиться в код:
/ Lib / дм-ядро / ассоциации / relationship.rb
# Eager load the collection using the source as a base
#
# @param [Collection] source
# the source collection to query with
# @param [Query, Hash] query
# optional query to restrict the collection
#
# @return [Collection]
# the loaded collection for the source
#
# @api private
def eager_load(source, query = nil)
targets = source.model.all(query_for(source, query))
# FIXME: cannot associate targets to m:m collection yet
if source.loaded? && !source.kind_of?(ManyToMany::Collection)
associate_targets(source, targets)
end
targets
end
. / Lib / dm-core / association / one_to_many.rb:
def lazy_load(source)
return if loaded?(source)
# SEL: load all related resources in the source collection
if source.saved? && (collection = source.collection).size > 1
eager_load(collection)
end
unless loaded?(source)
set!(source, collection_for(source))
end
end