Skip to content

Commit 097c228

Browse files
committed
WIP #1105 fix bug with inclusion and linkage of has_one polymorphic
1 parent 38757c6 commit 097c228

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Diff for: lib/jsonapi/relationship.rb

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def self.polymorphic_types(name)
4141
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
4242
(hash[reflection.options[:as]] ||= []) << klass.name.downcase
4343
end
44+
klass.reflect_on_all_associations(:has_one).select{|r| r.options[:as] }.each do |reflection|
45+
(hash[reflection.options[:as]] ||= []) << klass.name.downcase
46+
end
4447
end
4548
end
4649
end

Diff for: lib/jsonapi/resource.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,10 @@ def define_relationship_methods(relationship_name, relationship_klass, options)
10121012
def define_foreign_key_setter(relationship)
10131013
if relationship.polymorphic?
10141014
define_on_resource "#{relationship.foreign_key}=" do |v|
1015-
_model.method("#{relationship.foreign_key}=").call(v[:id])
1016-
_model.public_send("#{relationship.polymorphic_type}=", v[:type])
1015+
model_id = v.nil? ? nil : v[:id]
1016+
model_type = v.nil? ? nil : self.class.resource_klass_for(v[:type].to_s)._model_class.to_s
1017+
_model.method("#{relationship.foreign_key}=").call(model_id)
1018+
_model.public_send("#{relationship.polymorphic_type}=", model_type)
10171019
end
10181020
else
10191021
define_on_resource "#{relationship.foreign_key}=" do |value|

0 commit comments

Comments
 (0)