lib/graphiti/util/persistence.rb in graphiti-1.0.rc.8 vs lib/graphiti/util/persistence.rb in graphiti-1.0.rc.9

- old
+ new

@@ -10,20 +10,20 @@ @meta = meta @attributes = attributes @relationships = relationships @caller_model = caller_model - @attributes.each_pair do |key, value| - @attributes[key] = @resource.typecast(key, value, :writable) - end - # Find the correct child resource for a given jsonapi type if meta_type = @meta[:type].try(:to_sym) if @resource.type != meta_type && @resource.polymorphic? @resource = @resource.class.resource_for_type(meta_type).new end end + + @attributes.each_pair do |key, value| + @attributes[key] = @resource.typecast(key, value, :writable) + end end # Perform the actual save logic. # # belongs_to must be processed before/separately from has_many - @@ -78,9 +78,12 @@ # This is not the case for HABTM, whose "foreign key" is a join table def update_foreign_key(parent_object, attrs, x) return if x[:sideload].type == :many_to_many if [:destroy, :disassociate].include?(x[:meta][:method]) + if x[:sideload].polymorphic_has_many? + attrs[:"#{x[:sideload].polymorphic_as}_type"] = nil + end attrs[x[:foreign_key]] = nil update_foreign_type(attrs, x, null: true) if x[:is_polymorphic] else if x[:sideload].polymorphic_has_many? attrs[:"#{x[:sideload].polymorphic_as}_type"] = parent_object.class.name