Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior when object belongs_to :size #459

Closed
gkpacker opened this issue Apr 2, 2020 · 2 comments
Closed

Unexpected behavior when object belongs_to :size #459

gkpacker opened this issue Apr 2, 2020 · 2 comments

Comments

@gkpacker
Copy link

gkpacker commented Apr 2, 2020

Hello!

I'm trying to serialize my product object but it responds to size, so it is treated as a collection in:

    def is_collection?(resource, force_is_collection = nil)
      return force_is_collection unless force_is_collection.nil?

      resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
    end

and ends up breaking the code:

[1] pry(#<Api::V1::ProductsController>)> ProductSerializer.new(@product).serializable_hash
NoMethodError: undefined method `each' for #<Product:0x00007f8f563881b8>

It is intended to be that way?

Workaround:

ProductSerializer.new(@product, is_collection: false).serializable_hash

Thanks!

@stas
Copy link
Contributor

stas commented Apr 11, 2020

This is now merged in the forked project!

@gkpacker
Copy link
Author

gkpacker commented Jun 3, 2020

Nice, I'll close the issue!

@gkpacker gkpacker closed this as completed Jun 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants