ar_lazy_preload 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 21e63192d8ab41a89c7fa7fe49300123e7f8759d7e3270fdf4d821bc9865db8b
|
|
4
|
+
data.tar.gz: 28485e31dd75c9c93949ff9cc7a5a284857d347892a58a050cf754f50204beff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 34932cd853c5344aa865da00c1210a0680e0b18e3ac4994a9908d16683d97905deaaf56b4cd2963ef01c20352df63f80fd4021d97a1831a4691129c5382c4a35
|
|
7
|
+
data.tar.gz: d40a3cf3c8ef27508e385649a4373fe84fca14796c9cd44fdb7b6df55123f9b09f9bf00d9fcfb255ba9b3c8844633e05968a50297cc6867f1ce21664e4a45944
|
data/README.md
CHANGED
|
@@ -13,7 +13,10 @@ Used in production by:
|
|
|
13
13
|
- Toptal
|
|
14
14
|
- _Want to be here? Let me know_ 🙂
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
## Professional Support
|
|
17
|
+
|
|
18
|
+
Dealing with performance issues in your Rails app beyond N+1 queries?
|
|
19
|
+
I'm available for consulting — [get in touch](https://dmitrytsepelev.dev/consulting).
|
|
17
20
|
|
|
18
21
|
## Why should I use it?
|
|
19
22
|
|
|
@@ -64,6 +67,10 @@ posts = User.preload_associations_lazily.flat_map(&:posts)
|
|
|
64
67
|
# => SELECT * FROM posts WHERE user_id in (...)
|
|
65
68
|
```
|
|
66
69
|
|
|
70
|
+
## Companion gem for non-association N+1s
|
|
71
|
+
|
|
72
|
+
[`N1Loader`](https://github.com/djezzzl/n1_loader) complements ArLazyPreload for batched lazy loading in places where regular ActiveRecord associations are not available (for example, computed values, custom loaders, service/API calls, and GraphQL fields). ArLazyPreload can stay focused on association loading while N1Loader handles the remaining N+1-prone derived data.
|
|
73
|
+
|
|
67
74
|
## Gotchas
|
|
68
75
|
|
|
69
76
|
1. Lazy preloading [does not work](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/40/files) for ActiveRecord < 6 when `.includes` is called earlier:
|
|
@@ -44,7 +44,7 @@ module ArLazyPreload
|
|
|
44
44
|
#
|
|
45
45
|
# Same effect can be achieved by User.lazy_preload(posts: :comments)
|
|
46
46
|
def preload_associations_lazily
|
|
47
|
-
|
|
47
|
+
tap { |relation| relation.preloads_associations_lazily = true }
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
# Specify relationships to be loaded lazily when association is loaded for the first time. For
|
|
@@ -23,7 +23,7 @@ module ArLazyPreload
|
|
|
23
23
|
|
|
24
24
|
# Takes all the associated records for the records, attached to the :parent_context and creates
|
|
25
25
|
# a preloading context for them
|
|
26
|
-
# rubocop:disable Metrics/MethodLength
|
|
26
|
+
# rubocop:disable Metrics/MethodLength
|
|
27
27
|
def perform
|
|
28
28
|
associated_records = parent_context.records.flat_map do |record|
|
|
29
29
|
next if record.nil?
|
|
@@ -32,7 +32,7 @@ module ArLazyPreload
|
|
|
32
32
|
next if reflection.nil?
|
|
33
33
|
|
|
34
34
|
record_association = record.association(association_name)
|
|
35
|
-
|
|
35
|
+
record_association.target
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
Context.register(
|
|
@@ -41,7 +41,7 @@ module ArLazyPreload
|
|
|
41
41
|
auto_preload: parent_context.auto_preload?
|
|
42
42
|
)
|
|
43
43
|
end
|
|
44
|
-
# rubocop:enable Metrics/MethodLength
|
|
44
|
+
# rubocop:enable Metrics/MethodLength
|
|
45
45
|
|
|
46
46
|
private
|
|
47
47
|
|
|
@@ -90,7 +90,7 @@ module ArLazyPreload
|
|
|
90
90
|
def collect_intermediate_records(klass_records, reflection)
|
|
91
91
|
klass_records.flat_map do |record|
|
|
92
92
|
record_association = record.association(reflection.name)
|
|
93
|
-
|
|
93
|
+
record_association.target
|
|
94
94
|
end.compact
|
|
95
95
|
end
|
|
96
96
|
|