0

我有 rails 4.2 和 ruby​​ 2.3.5,我正在用 ruby​​ 2.6.1 将它升级到 rails 5.2。

使用较旧的 rails 版本(4.2),我的 UserAuthenticatedSerializer 工作正常并且给出了正确的响应。

升级到 rails 5.2 后,我的 Serializer 开始抛出参数错误。

我对其进行了调试,它是从 relationship.rb 文件方法名称 fetch_id 和 fetch_associated_object 中抛出的。在这两种方法中,object_block.call(record, params) 都会抛出一个参数错误。

如果我从参数中删除第二个参数,它工作正常。传递这两个参数会导致错误。

同样的关联适用于 rails 4.2,但不适用于 rails 5.2。

这是我的代码快照:

response = UserAuthenticatedSerializer.new(@user, { params: { domain: current_domain } }).to_json
error = ArgumentError (wrong number of arguments (given 1, expected 0))

user_authenticated_serializer.rb ===>
class UserAuthenticatedSerializer
include FastJsonapi::ObjectSerializer
has_one :user_profile, serializer: UserProfileSerializer, &:user_profile
has_many :user_topic_label_order, &:user_topic_label_order
end

user.rb ====>
Relationship in user model:
has_one :user_profile, dependent: :destroy
has_many :user_topic_label_order, dependent: :destroy

Rails 版本:5.2.2 Ruby 版本:ruby 2.6.1 fast_jsonapi gem 版本:fast_jsonapi (1.5) active_model_serializers (~> 0.10.9)

4

1 回答 1

0

该错误来自您的序列化程序,即来自将额外的参数传递给has_one. 试试这个:

class UserAuthenticatedSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :address

  has_one :user_profile #, &:user_profile
end

你不需要传递任何额外的东西。

于 2019-06-13T11:09:19.263 回答