Skip to content

Commit da982ff

Browse files
committed
fix: bug when nesting non-existing relations
1 parent fc073be commit da982ff

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/auto_preload/resolver.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ def split_inclusions(model, inclusions, root: false)
9595

9696
head, *tail = inclusions.split(".", 2)
9797
head = head.strip.underscore.to_sym
98-
child_model = find_association(model, head, root: root).klass
99-
[{ head => resolve(child_model, tail[0]) }]
98+
child_model = find_association(model, head, root: root)&.klass
99+
if child_model.nil?
100+
[]
101+
else
102+
[{ head => resolve(child_model, tail[0]) }]
103+
end
100104
end
101105

102106
# @param model [ActiveRecord::Base]

spec/auto_preload/serializer_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
expect(subject.resolve(Comment, "user,article")).to eq [:user]
1919
end
2020

21+
it "skips not defined associations in the serializer when it's nested" do
22+
expect(subject.resolve(Comment, "user.something.something.something,article")).to eq([{ user: [] }])
23+
end
24+
2125
it "prevents loops" do
2226
expect(subject.resolve(Comment, "**")).to eq [:user]
2327
end

0 commit comments

Comments
 (0)