I have an issue with jsonapi-rails (v0.4.0) and rendering relationships. I have a simple model consisting of Challenge and ChallengeAnswer; a Challenge has_many ChallengeAnswers, and a ChallengeAnswer has a value. Here are my serializable classes:
class SerializableChallenge < JSONAPI::Serializable::Resource
type "challenges"
has_many :answers do
data do
puts "SerializableChallenge: custom answers relation called"
@object.challenge_answers
end
end
end
class SerializableChallengeAnswer < JSONAPI::Serializable::Resource
type "challenge_answers"
belongs_to :challenge
attribute :value do
puts "SerializableChallengeAnswer: custom value attribute called"
@object.value
end
end
And this is the render call in my controller action:
render jsonapi: challenges, include: [:answers]
Now I would except that my result JSON contains a relation named answers, and the relation contains elements with a value. Instead, this is what I get:
"relationships": {
"answers": {
"data":[
{
"type": "challenge_answers",
"id": "cb0afe1f-fe68-4eb1-9454-8b56f419a4e9"
}
]
}
}
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.
I have an issue with
jsonapi-rails(v0.4.0) and rendering relationships. I have a simple model consisting ofChallengeandChallengeAnswer; aChallengehas_manyChallengeAnswers, and aChallengeAnswerhas 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
idis 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
valueattribute 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.