-
Notifications
You must be signed in to change notification settings - Fork 63
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
Fields of relationship objects are missing in result JSON #111
Comments
@drbarto Were you able to resolve this? I think that in your example changing your I am trying to resolve a similar issue with multiple levels of relationships in my app that doesn't involve having to look up objects in the |
@patricklewis thanks for the hint! I'm not working on that codebase anymore, so unfortunately I cannot give your suggestion a try -- but hopefully it will be helpful to others :) IIRC I ended up writing a custom attribute which returned the nested data:
While I would have preferred the nice declarative form, this also worked and even made it simpler to generate the output format which I was after. |
@drbarto Nice.. I also ended up using a similar delegation approach in my application after continuing to have trouble with multiple levels of nested relationships in the jsonapi call. This is what I ended up with: controller render(
jsonapi: outcome.result,
include: [:messages],
fields: {
messages: %i[body recipient_names sender_name]
} 'message' serializer attribute :sender_name do
@object.sender_name
end
attribute :recipient_names do
@object.recipients.includes(:person).map(&:name)
end As you noted, this gave me more control over the format of the response and seems to be the best workaround. Thanks! |
I have an issue with
jsonapi-rails
(v0.4.0) and rendering relationships. I have a simple model consisting ofChallenge
andChallengeAnswer
; aChallenge
has_many
ChallengeAnswer
s, and aChallengeAnswer
has avalue
. Here are my serializable classes:And this is the render call in my controller action:
Now I would except that my result JSON contains a relation named
answers
, and the relation contains elements with avalue
. Instead, this is what I get:So the relationship's data is correctly looked up (the
id
is correct), but the data fields (i.e.value
) are missing.As you can see in my serializable classes above, I added log messages, and both messages are logged. This means that my custom
value
attribute implementation is executed, but its value is not included in the result.I tried various fixes, e.g. using
fields: { answers: [:value] }
, but without success. Can someone point out what I'm doing wrong here? Maybe I'm getting the whole point of "relationship" wrong.The text was updated successfully, but these errors were encountered: