【发布时间】:2016-07-13 22:56:26
【问题描述】:
我的控制器中有一个方法可以查找作为 json 传入的模型的关系。当所有预期的属性都存在时,它工作正常。但是如果我排除一个“项目”,我的项目方法会给我第一条记录而不是 nil 或 Project.none 。
所以当
ActiveModelSerializers::Deserialization.jsonapi_parse(relationship_params['project'] 计算结果为 {}。调用 project 返回 Project.first 的等效项
项目方法
def project
Project.find_by(ActiveModelSerializers::Deserialization.jsonapi_parse(relationship_params['project'], only: [:id]))
end
关系参数
def relationship_params
return false unless params.dig(:data, :relationships)
params.require(:data).require(:relationships).transform_keys(&:dasherize)
end
【问题讨论】:
标签: ruby-on-rails activerecord active-model-serializers