Rails Active Model Serializer包含在控制器中不注入包含参数中的多个关系

时间:2017-04-27 15:36:27

标签: 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

和以下控制器代码:

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 应仅作为参考出现:{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的引用添加到 relationships 片段。这是因为在活动模型序列化程序中,角色是 has_many 关系,而 current_scenario belongs_to 吗?我理解错了json-api适配器规范吗?

非常感谢!

3 个答案:

答案 0 :(得分:1)

哎哟。 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"
    }
}]

}

答案 1 :(得分:0)

抱歉这个错误。当没有找到序列化程序时,我们还没有弄清楚如何确定关系的类型。如果你有例外会更有帮助吗?

答案 2 :(得分:-1)

这就是JSON API的工作方式,你无法在'一个对象'中检索所需的模型+关系。关系总是分开的。所以你需要以某种方式“粘合”它。您可以使用可以帮助您的gem,或者您可以在前端执行此操作(所有大型框架都支持它)。 一般来说,这种带有“关系”的方法看起来很奇怪,但是当你拥有大量依赖的复杂对象时,这是唯一有效的方法。