Skip to content

Commit b80d815

Browse files
byroothcmaATshopify
authored andcommitted
Add AR::Base#load_async(*associations) as a nicer API
1 parent 8b1f29e commit b80d815

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def association(name) # :nodoc:
6262
association
6363
end
6464

65+
def load_async(*associations) # TODO: doc
66+
associations.map { |name| association(name) }.each(&:async_load_target)
67+
self
68+
end
69+
6570
def association_cached?(name) # :nodoc:
6671
@association_cache.key?(name)
6772
end

activerecord/lib/active_record/associations/association.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def async_load_target
191191
@target = find_target(async: true) if (@stale_state && stale_target?) || find_target?
192192

193193
loaded! unless loaded?
194-
@target
194+
nil
195195
end
196196

197197
# We can't dump @reflection and @through_reflection since it contains the scope proc
@@ -247,7 +247,7 @@ def find_target(async: false)
247247
if async
248248
return scope.load_async.then(&:to_a)
249249
else
250-
return scope.to_a
250+
return scope.to_a
251251
end
252252
end
253253

activerecord/lib/active_record/core.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,11 @@ def cached_find_by(keys, values)
432432
where(wheres).limit(1)
433433
}
434434

435-
begin
436-
statement.execute(values.flatten, lease_connection, allow_retry: true).then(&:first)
437-
rescue TypeError
438-
raise ActiveRecord::StatementInvalid
435+
begin
436+
statement.execute(values.flatten, lease_connection, allow_retry: true).then(&:first)
437+
rescue TypeError
438+
raise ActiveRecord::StatementInvalid
439+
end
439440
end
440441
end
441442
end

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ def test_async_load_belongs_to
18511851
client = Client.find(3)
18521852
first_firm = companies(:first_firm)
18531853

1854-
promise = client.association(:firm).async_load_target
1854+
promise = client.load_async(:firm)
18551855
wait_for_async_query
18561856

18571857
events = []

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3263,7 +3263,7 @@ class AsyncHasOneAssociationsTest < ActiveRecord::TestCase
32633263
def test_async_load_has_many
32643264
firm = companies(:first_firm)
32653265

3266-
promise = firm.association(:clients).async_load_target
3266+
promise = firm.load_async(:clients)
32673267
wait_for_async_query
32683268

32693269
events = []

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def test_async_load_has_one
955955
firm = companies(:first_firm)
956956
first_account = Account.find(1)
957957

958-
promise = firm.association(:account).async_load_target
958+
promise = firm.load_async(:account)
959959
wait_for_async_query
960960

961961
events = []

0 commit comments

Comments
 (0)