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

Undefined method serialize_to_hash on Serializer page #53

Open
CodingItWrong opened this issue Sep 27, 2020 · 8 comments
Open

Undefined method serialize_to_hash on Serializer page #53

CodingItWrong opened this issue Sep 27, 2020 · 8 comments

Comments

@CodingItWrong
Copy link

guide/serializer.html suggests that we can do:

post = Post.find(1)
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, nil))

But when I do the equivalent in 0.10.2, I get the error:

undefined method `serialize_to_hash'
 for #<JSONAPI::ResourceSerializer:0x00007fae9ec1cbf0>

Looking through the source of JSONAPI::ResourceSerializer, #object_hash seems to be the currently-available method that meets that need. The code succeeds for me with the following change:

post = Post.find(1)
JSONAPI::ResourceSerializer.new(PostResource).object_hash(PostResource.new(post, nil), nil)

(Note that #object_hash takes two arguments, so we pass nil for the second.)

I would PR this change to the page, but I'm unsure of the correct changes for the rest of the page.

@soltex1
Copy link

soltex1 commented Aug 11, 2021

@CodingItWrong Did you find the issue?

@CodingItWrong
Copy link
Author

@soltex1 I didn't figure out the correct changes to make on the page, no, so I wasn't able to do a PR to update it. As for the issue in the code itself, the alternate approach I mentioned above did work.

@soltex1
Copy link

soltex1 commented Aug 11, 2021

Yes, I can confirm that using object_hash instead of serialize_to_hash works. Not sure why a very simple example doesn't work as mentioned in the documentation. Anyway, thanks for your quick reply! :)

@soltex1
Copy link

soltex1 commented Aug 11, 2021

@CodingItWrong Well, it looks like the version 0.10.2 and even the latest one 0.10.5 doesn't contain the method serialize_to_hash, but the master does.

The documentation tells us to add gem 'jsonapi-resources' to the Gemfile which is going to install the 0.10.5, and then they use the method serialize_to_hash in their examples that doesn't exist on that version.

We might have to install the gem directly from the github by adding this line to the Gemfile:

gem 'jsonapi-resources', :git => 'https://github.com/cerebris/jsonapi-resources.git'

Unfortunately, I have a few conflicts with other gems and I wasn't able to test if this works.

@womanonrails
Copy link

Here is the issue about this problem on jsonapi-resources GitHub.

@benbonnet
Copy link

@soltex1 can confirm it works with gem 'jsonapi-resources', :git => 'https://github.com/cerebris/jsonapi-resources.git'

@HenrichJones
Copy link

@benbonnet worked for me!

@Genkilabs
Copy link

Genkilabs commented Jan 4, 2023

For those wanting to use '~> 0.10.7' (in my case Rails 5.2.8), you can swap out serialize_to_hash with object_hash but it requires 2 params. The second can be an empty hash.
For a nice TLDR drop-in, check out @xhocquet solution over on the 1149 issue:
cerebris/jsonapi-resources#1149 (comment)

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

6 participants