0

我在一个经典的 Rails 项目中工作,该项目包含自己的移动设备 API。

当我发送 JSON 时,模型拥有我的 API 的所有属性,例如具有嵌套用户配置文件属性的用户对象,它工作正常。

JSON 看起来像这样:

{
  "email": "user-1@example.com",
  "user_profile_attributes": {
    "display_name": "Awesome user",
    "field_a": "string",
    "field_b": "string"
  }
}

当我现在field_a从我的 JSON 请求中删除一个字段时,我希望 Rails 后端在使用updateActiveRecord 方法更新数据库记录时会忽略该字段。不幸的是,ActiveRecord 决定取消我丢失的字段。

我发送的 JSONfield_a看起来像这样:

{
  "email": "user-1@example.com",
  "user_profile_attributes": {
    "display_name": "Awesome user",
    "field_b": "string"
  }
}

INSERT我的日志文件中的 显示该字段设置为,并且NULL在我的数据库中,该字段也设置为NULLupdate我的params.

我想知道的是,这是否是正确的行为,以及在常规Rails API only项目中的行为是否相同。如何防止 Rails 或 ActiveRecord写入NULL数据库中的字段,这些字段在我对 API 发布的 JSON 请求中不存在?另外,当它不是我的请求 JSON 的一部分时,如何防止 Rails 或 ActiveRecord 删除嵌套关系对象?例如,如果您删除整个user_profile_attributes节点,ActiveRecord 将删除它。

我的控制器中的更新方法如下所示:

def update
  respond_to do |format|
    if @current_user.update(update_user_params)
      format.json { render 'api/app/v1/users/show', status: :ok, locals: { user: @current_user } }
    else
      render json: @current_user.errors, status: :unprocessable_entity
    end
  end
end

def update_user_params
  params.require(:user).permit(
    :email,
    :password,
    user_profile_attributes: [:display_name, :field_a, :field_b]
  )
end

为了更好地演示代码,我使用 Rails 6 编写了一个示例项目,它与我在项目中所做的相同。它还包括一个openapi.yml用于 Pawn、Insomnia 或 Postman 的/api/app/v1轻松测试项目 API。

我使用的更新方法的代码在我的那个位置users_controller.rbhttps ://github.com/fuxx/update-db-question/blob/master/app/controllers/api/app/v1/users_controller.rb#L16

我的示例项目位于 GitHub 上: https ://github.com/fuxx/update-db-question

提前致谢!

4

1 回答 1

1

尝试在您的用户模型中更改accepts_nested_attributes_for :user_profile为。accepts_nested_attributes_for :user_profile, update_only: true

于 2020-06-30T10:30:07.190 回答