Skip to content

Commit 8f70811

Browse files
authored
Avoid AR bug on has_one associations (#87)
* Avoid AR bug with has_one Described here: rails/rails#32886 * bump version
1 parent f56d399 commit 8f70811

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/jsonapi_compliable/adapters/active_record_sideloading.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ def has_one(association_name, scope: nil, resource:, foreign_key:, primary_key:
5858
parent.association(association_name).loaded!
5959
relevant_child = children.find { |c| c.send(foreign_key) == parent.send(primary_key) }
6060
next unless relevant_child
61-
parent.association(association_name).replace(relevant_child, false)
61+
62+
# Use private methods because of Rails bug
63+
# https://github.com/rails/rails/issues/32886
64+
association = parent.association(association_name)
65+
association.send(:set_owner_attributes, relevant_child)
66+
association.send(:set_inverse_instance, relevant_child)
67+
association.send(:target=, relevant_child)
6268
end
6369
end
6470

lib/jsonapi_compliable/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonapiCompliable
2-
VERSION = "0.11.12"
2+
VERSION = "0.11.13"
33
end

0 commit comments

Comments
 (0)