ruby-on-rails - Rails Active Model Serializer 包含在 Controller 中不注入(inject)在包含的参数中有很多关系

标签 ruby-on-rails ruby-on-rails-5 active-model-serializers json-api

我看到有关 rails 5、事件模型序列化程序和 json-api 适配器的奇怪行为。

给定以下带有 Rolify gem 的用户模型:

class User < ActiveRecord::Base
  #
  # Gem Includes
  #
  rolify


  # Include devise modules.
  devise :database_authenticatable,
      :recoverable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User

  #
  # Callbacks
  #
  after_create :assign_default_role

  #
  # Attributes
  #
  attr_accessor :remote_image

  #
  # Validations
  #
  validates :name, presence: true, length: {in: 1..100}
  validates :last_name, presence: true, length: {in: 1..100}
  validates :role_ids, presence: true, on: :update

  #
  # Relations
  #
  belongs_to :current_scenario, class_name: "Scenario"


  #
  # Private Instance Methods
  #
  def assign_default_role
    self.add_role(:user) if self.roles.blank?
  end

end

和以下 Controller 代码:
def show
  @user = User.find(params[:id])
  authorize @user
  render json: @user, include: ['roles'], status: :ok
end

如您所见,我将要呈现的角色关系作为 json api 响应的一部分,使用 json-api 适配器格式。

仅供引用,UserSerializer:
class UserSerializer < ActiveModel::Serializer
  #
  # Attributes
  #
  attributes :id, :email, :name, :last_name, :image_url, :image_thumb_url, :created_at, :updated_at, :current_scenario_id, :last_sign_in_at

  #
  # Relations
  #
  has_one :current_scenario
  has_many :roles

  #
  # Methods
  #
  def image_url
    object.image_url
  end

  def image_thumb_url
    object.image_url(:thumb)
  end
end

检索 json 响应时,我得到以下信息:
{
  "data": {
    "id":"2",
    "type":"users",
    "attributes": {
      "email":"talvarez@igaltex.com.ar", ...
    },
    "relationships": {
      "current-scenario": {
        "data": {
          "id":"204",
          "type":"scenarios"
        }
      },
      "roles": {
        "data": [
          {
            "id":1,
            "name":"user",
            "resource-type":null,
            "resource-id":null,
            "created-at":"2017-01-23T10:27:08.707-03:00",
            "updated-at":"2017-01-23T10:27:08.707-03:00"
          },
          {
            "id":2,
            "name":"admin",
            "resource-type":null,
            "resource-id":null,
            "created-at":"2017-01-24T09:40:53.020-03:00",
            "updated-at":"2017-01-24T09:40:53.020-03:00"
          }
        ]
      }
    }
  }
}

如您所见,包含的关系角色及其所有属性位于 json-api 响应的关系片段中。角色数据不应该在包含的片段中吗?顺便说一句,这是缺失的?此外,在关系片段中,角色应该仅作为引用出现,例如:{relationships: {roles: [{id: "1", type: "role"}, {id: "2", type: "role"}]}我错了吗?

为了对比这一点,看看当还包含 current_scenario 时会发生什么。关系:
{
  "data": {
    "id":"2",
    "type":"users",
    "attributes": {
      "email":"talvarez@igaltex.com.ar",
      "name":"Tomás",
      "last-name":"Alvarez", 
      ...
    },
    "relationships": {
      "current-scenario": {
        "data": {
          "id":"204",
          "type":"scenarios"
        }
      },
      "roles": {
        "data": [
          {
            "id":1,
            "name":"user",
            "resource-type":null,
            ...
          }
        ]
      }
    },
    "included": [
      {
        "id":"204",
        "type":"scenarios",
        "attributes": {
          "name":"Scenario reload II",
          "description":null,
          "created-at":"2017-04-18T11:55:35.242-03:00",
          "updated-at":"2017-04-18T11:55:35.242-03:00"
        },
        "relationships": {
          "scenario-stocks": {
            "data":[]
          }
        }
      }
    ]
  }
}

看看现在包含的片段如何显示有关 current_scenario 的所有信息,并且只有对 current_scenario 的引用被添加到关系片段中。这是因为角色是事件模型序列化程序中的 has_many 关系,而 current_scenario 是 belongs_to 吗?我是否理解错误的 json-api 适配器规范?

非常感谢!

最佳答案

哎哟。 JSON-API 中的不一致响应是因为我忘记在后端添加角色模型序列化程序(Rails 5)。这是现在的 json 响应,这是我正在寻找的:

{
"data": {
    "id": "2",
    "type": "users",
    "attributes": {
        "email": "talvarez@igaltex.com.ar",
        "name": "Tomás",
        "last-name": "Alvarez",
        "image-url": "http://localhost:3001/uploads/user/image/2/05a4dc7.jpg",
        "image-thumb-url": "http://localhost:3001/uploads/user/image/2/thumb_05a4dc7.jpg",
        "created-at": "2017-01-23T10:39:12.485-03:00",
        "updated-at": "2017-04-25T16:32:14.610-03:00",
        "current-scenario-id": 204,
        "last-sign-in-at": "2017-04-25T16:29:03.559-03:00"
    },
    "relationships": {
        "current-scenario": {
            "data": {
                "id": "204",
                "type": "scenarios"
            }
        },
        "roles": {
            "data": [{
                "id": "1",
                "type": "roles"
            }, {
                "id": "2",
                "type": "roles"
            }]
        }
    }
},
"included": [{
    "id": "204",
    "type": "scenarios",
    "attributes": {
        "name": "Scenario reload II",
        "description": null,
        "created-at": "2017-04-18T11:55:35.242-03:00",
        "updated-at": "2017-04-18T11:55:35.242-03:00"
    },
    "relationships": {
        "scenario-stocks": {
            "data": []
        }
    }
}, {
    "id": "1",
    "type": "roles",
    "attributes": {
        "name": "user"
    }
}, {
    "id": "2",
    "type": "roles",
    "attributes": {
        "name": "admin"
    }
}]

}

关于ruby-on-rails - Rails Active Model Serializer 包含在 Controller 中不注入(inject)在包含的参数中有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43662168/

相关文章:

ruby-on-rails - 防止 Rails 2/3 缓存 Lib/Classes

html - 将 HTML 转换为 markdown

heroku - Rails : application. css 不在 Assets 管道中

ruby-on-rails - Rails,如何指定在 Controller 的 JSON 响应中输出哪些字段?

ruby-on-rails - 避免 Rails 多表查询中的 N+1 查询

caching - 如何通过 ActiveModel::Serializers (v0.10.0.rc1) 使用缓存键的动态值

ruby-on-rails - 使用 ActiveModel::Serializer 如何向每个模型添加表属性

ruby-on-rails - 创建 Rails 新应用程序项目时出错

ruby-on-rails - 回形针:如何更改 "#"修饰符行为?

ruby-on-rails - 日期更新失败,但更新返回 true (Rails ActiveRecord)