instance method load_async

Ruby on Rails 8.0.4

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

load_async()

Schedule the query to be performed from a background thread pool.

Post.where(published: true).load_async # => #<ActiveRecord::Relation>

When the Relation is iterated, if the background query wasn’t executed yet, it will be performed by the foreground thread.

Note that config.active_record.async_query_executor must be configured for queries to actually be executed concurrently. Otherwise it defaults to executing them in the foreground.

If the query was actually executed in the background, the Active Record logs will show it by prefixing the log line with ASYNC:

ASYNC Post Load (0.0ms) (db time 2ms)  SELECT "posts".* FROM "posts" LIMIT 100
Source
# File activerecord/lib/active_record/relation.rb, line 1148
    def load_async
      with_connection do |c|
        return load if !c.async_enabled?

        unless loaded?
          result = exec_main_query(async: !c.current_transaction.joinable?)

          if result.is_a?(Array)
            @records = result
          else
            @future_result = result
          end
          @loaded = true
        end
      end

      self
    end

Defined in activerecord/lib/active_record/relation.rb line 1148 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Relation

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close