Skip to content

Commit 3b49e47

Browse files
p8rafaelfranca
andcommitted
Use includes instead of eager_load for with_all_rich_text
`eager_load` performs a single query using a `LEFT OUTER JOIN` to load the associations. Loading the associations in a join can result in many rows that contain redundant data and it performs poorly at scale. With `includes` a separate query is performed for each association, unless a join is required by conditions. Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent af2bbd5 commit 3b49e47

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

actiontext/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Use `includes` instead of `eager_load` for `with_all_rich_text`.
2+
3+
*Petrik de Heus*
4+
15
* Delegate `ActionText::Content#deconstruct` to `Nokogiri::XML::DocumentFragment#elements`
26

37
```ruby

actiontext/lib/action_text/attribute.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def #{name}=(body)
6565

6666
# Eager load all dependent RichText models in bulk.
6767
def with_all_rich_text
68-
eager_load(rich_text_association_names)
68+
includes(rich_text_association_names)
6969
end
7070

7171
def rich_text_association_names

actiontext/test/unit/model_test.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,18 @@ class ActionText::ModelTest < ActiveSupport::TestCase
106106
end
107107

108108
test "eager loading all rich text" do
109-
Message.create!(subject: "Subject", content: "<h1>Content</h1>", body: "<h2>Body</h2>")
109+
2.times do
110+
Message.create!(subject: "Subject", content: "<h1>Content</h1>", body: "<h2>Body</h2>")
111+
end
112+
113+
message = assert_queries_count(3) do
114+
# 3 queries:
115+
# messages x 1
116+
# action texts (content) x 1
117+
# action texts (body) x 1
118+
Message.with_all_rich_text.to_a.last
119+
end
110120

111-
message = assert_queries_count(1) { Message.with_all_rich_text.last }
112121
assert_no_queries do
113122
assert_equal "Content", message.content.to_plain_text
114123
assert_equal "Body", message.body.to_plain_text

0 commit comments

Comments
 (0)