Skip to content

Getter of has_many or has_one relationship gets called twice #99

@wuarmin

Description

@wuarmin

Hello,
following test code illustrates the behaviour:

class Post
	attr_accessor :id, :title, :content, :likes

	def initialize(params)
		@id = params[:id]
		@title = params[:title]
		@content = params[:content]
		@likes = params[:likes]
	end

	def likes
		puts "likes called" # getter gets called twice.
		@likes
	end
end
class Like
	attr_accessor :id, :content

	def initialize(params)
		@id = params[:id]
		@content = params[:content]
	end
end
require 'jsonapi-serializers'

class PostSerializer
  include JSONAPI::Serializer

  attribute :title
  attribute :content
  has_many :likes, include_data: false 
end

class LikeSerializer
  include JSONAPI::Serializer

  attribute :content
end
post = Post.new({
	id: 1, 
	title: 'title', 
	content: 'content', 
	likes: [
		Like.new(id: 2, content: 'test'),
		Like.new(id: 3, content: 'test2')
	]
})

JSONAPI::Serializer.serialize(post, is_collection: false, include: ['likes'])

Is this the expected behaviour? Maybe an if-statement? I did not look deep in code yet.
Our problem is, that we have some time-consuming "getter"-functions and don't want to use/aren't able to use members.

Best regards!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions