用多态关系修补attachment
时,仅保存ID,而忽略类型。它是通过JSONAPI完成的,我认为它可能来自AMS
问题:
这是Rails接收更新JSON的方式:
{"data"=>{"id"=>"94", "attributes"=>{"file-file-name"=>"logo.png"}, "relationships"=>{"attachmentable"=>{"data"=>{"type"=>"ticket/replies", "id"=>"192"}}}, "type"=>"ticket/attachments"}, "id"=>"94"}
如您所见,关系类型被称为ticket\replies
。 Rails仅保存ID:UPDATE "ticket_attachments" SET "attachmentable_id" = $1, "updated_at" = $2 WHERE "ticket_attachments"."id" = $3
在静态页面的其他位置使用相同的关系,并且可以正确处理该关系。
为什么忽略类型?它与名称空间/模块有关吗?
相关代码:
这是使用的不同关系:
class Ticket::Reply < ApplicationRecord
has_many :attachments, as: :attachmentable, :dependent => :destroy
end
class Ticket::Attachment < ApplicationRecord
belongs_to :attachmentable, :polymorphic=>true
end
这是控制器动作和params方法
module Api::V1
module Ticket
class AttachmentsController < MarketadminController
def create
build_attachment
save_attachment
json_response @attachment
end
private
def build_attachment
@attachment ||= attachment_scope.build
@attachment.attributes=attachment_params
end
def attachment_params
ActiveModelSerializers::Deserialization.
jsonapi_parse(params)
end
end
end
end
编辑:将AMS更新到最新版本0.10.9,同样的问题