Commit bb295d1
committed
Relations:
Similar to #932 for associations, calling `#decorate` on a relation
loads the records every time, regardless of whether the underlying
relation has already been loaded.
This is because the default of passing `all` as the collection to
decorate ends up being a new relation, so even if it were previously
loaded, the new relation including `all` needs to be loaded.
`ActiveRecord::Associations::CollectionProxy` actually inherits from
`ActiveRecord::Relation`, so by pushing this up to the relation, we can
cover both scenarios.
https://github.com/rails/rails/blob/57c24948eb5cc9e5f9a4cecb6f2060f53e2246e1/activerecord/lib/active_record/associations/collection_proxy.rb#L31
The documentation included in #932 (`company.products.popular.decorate`)
wasn't correct as you're decorating a relation there (`popular`), not
just the association (`products`).
This now covers the association itself (`company.products.decorate`), as
well as a relation from that association (`company.products.popular.decorate`),
as well as a relation right from the class without associations
(`Product.popular.decorate`).
Decorating the class itself (`Product.decorate`) still calls `all` since
the model class is not a relation itself.#decorate queries DB every time1 parent 52ea163 commit bb295d1
6 files changed
Lines changed: 55 additions & 18 deletions
File tree
- lib/draper
- decoratable
- spec/dummy/spec/models
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
3 | 8 | | |
4 | 9 | | |
5 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
24 | 56 | | |
25 | 57 | | |
26 | 58 | | |
| |||
47 | 79 | | |
48 | 80 | | |
49 | 81 | | |
50 | | - | |
| 82 | + | |
51 | 83 | | |
52 | 84 | | |
53 | 85 | | |
| |||
0 commit comments