0

我正在尝试为序列化器中的对象实现关系,但是该关系不是实际的活动记录关联,这意味着没有称为发布者的表。这只是我想显示为关系的用户对象的一种方法。

class UserSerializer
  include JSONAPI::Serializer
  
  attributes :email, :username

  has_many :books
  has_many :publishers, serializer: PublisherSerializer do |user|
    user.publishers
  end
end

接着

UserSerilaizer.new(user, options).to_json

但是我不断收到“Publisher_ex”的未定义方法“id”:字符串我怎样才能使它工作?我更希望将发布者作为一种关系而不是一种属性,还是没有办法解决它?

4

0 回答 0