-
Notifications
You must be signed in to change notification settings - Fork 422
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
Sparse Fieldset and Have Relationships #304
Comments
@ritesh2741 You should be able to get the correct serialized output with just one call to the serializer. Have you tried something like this?
|
@shishirmk |
Should the relationships hash return EDIT More explicitly, the following code
generates* the following output
But if we change the fields option to include
The output includes associations in the relationships hash
I'm not sure if this should be considered a bug or not. Personally, I'd prefer if the relationships hash were implicitly populated with any included associations so that JSONAPI normalizers can always traverse the association graph easily. That said, using the sparse fields option kind of implies that you only want the serializer to return what you explicitly ask for. I'm not sure where the owners stand on this - if the current implementation is what you prefer, a small note in the documentation would help clarify *Note: I didn't actually run the example code to generate this output - I figured this issue out within my application and typed out the analogous JSON here. My apologies if there are any discrepancies with the actual output as a result. |
In given example with movie, actor and owner are 3 models with relationships
Let's say if I want only name from movie, and include only 1 attribute from actor and 1 attribute from owner, ( Let's assume actor has an attribute
rating
and owner has an attributefirst_name
as there aren't any given in example. )I can use
options[:include] = [:actor,:owner]
andMovieSerializer.new(movie, options).serializable_hash
to get the relationships and all the attributes that are present in all the modelsGiven the example for sparse fieldset,
MovieSerializer.new(movie, { fields: { movie: [:name] } })
picks only the movie name which is good.However I have some issues..
So, here are my problems and some workaround
There isn't an example of how i can select only some attributes from my relationships. So what I did was
options[:fields] = {:actor=>[:rating],:owner=>[:first_name]}
andMovieSerializer.new(movie, options).serializable_hash
gave me the required result. If this is correct please include this in the documentation.Given the example for sparse fieldset,
MovieSerializer.new(movie, { fields: { movie: [:name] } })
will return just the object with relationships as empty hash. I cannot have scenario as mentioned above and this one together. So i figured a "not so good" way to tackle thisoptions[:include] = [:actor,:owner]
options[:fields] = {:actor=>[:rating],:owner=>[:first_name]}
res1 = MovieSerializer.new(movie, options).serializable_hash
#returns the filtered response with proper fieldsetres2 = MovieSerializer.new(movie,{fields: {movie: [:name]}}).serializable_hash
#returns {} for relationshipsres2[:data][:relationships] = res1[:data][:relationships]
and the final result is in
res2
. However I am not sure if this is the right way. Please provide if there is a better way to do it.Reference: "
GET /articles?include=author&fields[articles]=title,body&fields[people]=name
" which is from json api docsThe text was updated successfully, but these errors were encountered: