representable icon indicating copy to clipboard operation
representable copied to clipboard

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

Open promisedlandt opened this issue 9 years ago • 8 comments

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

promisedlandt avatar Apr 03 '16 12:04 promisedlandt

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

apotonick avatar Apr 03 '16 22:04 apotonick

I mean the JSONAPI module.

promisedlandt avatar Apr 04 '16 07:04 promisedlandt

It's not even released yet :grin: 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?

apotonick avatar Apr 04 '16 08:04 apotonick

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 -_-

promisedlandt avatar Apr 04 '16 09:04 promisedlandt

https://gitter.im/trailblazer/chat ?

apotonick avatar Apr 04 '16 09:04 apotonick

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

apotonick avatar Apr 04 '16 09:04 apotonick

I'm in the chat.

promisedlandt avatar Apr 04 '16 09:04 promisedlandt

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

myabc avatar Jan 09 '17 22:01 myabc