Open ArneZsng opened 6 years ago
@lgebhardt Happy to provide the PR if you agree with the fix.
Just occured to me while updating from 0.9 to 0.10. Is there any chance that it will be fixed anytime soon?
Monkey-patched this and #1305 like this, but I haven't dived into JR internals too much to evaluate the risks of such patches:
def self._polymorphic_types
@poly_hash ||= {}.tap do |hash|
ActiveRecord::Base.descendants do |klass|
next unless Module === klass
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
(hash[reflection.options[:as]] ||= []) << klass.name.underscore
end
end
end
@poly_hash[_polymorphic_name.to_sym]
end
def _replace_polymorphic_to_one_link(relationship_type, key_value, key_type, _options)
relationship = self.class._relationships[relationship_type.to_sym]
send("#{relationship.foreign_key}=", type: key_type.to_s.classify, id: key_value)
@save_needed = true
:completed
end
This issue is a (choose one):
Checklist before submitting:
Description
Find the complete gist here: https://gist.github.com/ArneZsng/9a5542376d359755f62bbea5f16fb757 or below.
When creating a polymorphic relationship, the type is not properly converted anymore to a class name. This originally was the case in https://github.com/cerebris/jsonapi-resources/pull/288/files#diff-e6cf31c573bd5f2277b1f16812943df4R202 but was since removed with https://github.com/cerebris/jsonapi-resources/commit/bdcbf615155b8c6b3f34136d56d77254ed7d9cbf#diff-e6cf31c573bd5f2277b1f16812943df4L324
Instead of straight accepting the incoming type string, it should first be converted to a class name.
A fix would be to change
_replace_polymorphic_to_one_link
in https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource.rb to this:Bug Report
Output