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

.represent() does not work with collections and JSON API #187

Open
promisedlandt opened this issue Apr 3, 2016 · 8 comments
Open

.represent() does not work with collections and JSON API #187

promisedlandt opened this issue Apr 3, 2016 · 8 comments

Comments

@promisedlandt
Copy link

I'm trying to use Trailblazer to create a JSON API backend. Unfortunately, I can't get collections to work at all. This might be a mistake on my part, but I think I've tracked it down representable (but I'm not 100% sure).

What it boils down to is that .represent() does not work in JSON API when it's handed a collection.

This works:
Author::Representer::Index.for_collection.new(Author.all).to_json

This does not work:
Author::Representer::Index.represent(Author.all).to_json

I think that should work, right?

I have created a very simple Rails application to demonstrate:
https://github.com/promisedlandt/trailblazerjsonapi

@apotonick
Copy link
Member

When you say "JSON API", do you mean the JSONAPI module or a random JSON API?

@promisedlandt
Copy link
Author

I mean the JSONAPI module.

@apotonick
Copy link
Member

It's not even released yet 😁 haha! Is everything else working as expected? I will get back to the Roar 1.1 release, soon. Why is this posted on Representable, then, and not Roar, NILS?

@promisedlandt
Copy link
Author

Damn, it's unreleased? That explains the next 3 issues in ran into.

I didn't know whether the problem was in trailblazer, roar, representable or the docs -_-

@apotonick
Copy link
Member

@apotonick
Copy link
Member

I need the feedback, man, I can't just magically find out what's not working in the current version! 😉

@promisedlandt
Copy link
Author

I'm in the chat.

@myabc
Copy link

myabc commented Jan 9, 2017

This is an issue with Trailblazer 1.1.x (not sure if Trailblazer 2.0 is affected) and not Representable/ROAR JSON API.

The represent method currently expects an instance of the Array class, which Author.all is not (it's an ActiveRecord::Relation).

Either of the the following should work:

Author::Representer::Index.represent(Author.all.to_a).to_json
Author::Representer::Index.represent(Author.all, ActiveRecord::Relation).to_json

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

3 participants