Skip to content

has_one block optional returns error #263

@krzkrzkrz

Description

@krzkrzkrz

I have the following defined in a serializer:

class ArticleSerializer
  include FastJsonapi::ObjectSerializer

  set_key_transform :dash

  set_type : :articles

  has_one :last_chat_message, record_type: :'chat-messages', optional: true, serializer: ChatMessage do |object, params|
    object.chat_messages.where(priority: params[filter][priority]).last
  end
end

ChatMessageSerializer looks like:

class ChatMessageSerializer
  include FastJsonapi::ObjectSerializer

  set_key_transform :dash

  set_type : 'chat-messages'

  attributes :message, :created_at, :updated_at

  belongs_to :article
  belongs_to :user
end

ArticlesSerializer.new(Article.find(1), {}).serialized_json returns with:

NoMethodError: undefined method `id' for nil:NilClass
from /Users/foobar/.rbenv/versions/2.3.6/lib/ruby/gems/2.3.0/gems/fast_jsonapi-1.2/lib/fast_jsonapi/serialization_core.rb:185:in `fetch_id'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions