-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
json serializer ignores nested associations #921
Comments
If you're using the JSON adapter then it is the expected behavior. The JSON adapter only render the attributes of associations (see here) |
Yes, you are right. I use json adapter. |
If you need a workaround, you can do this: class UserSerializer < ActiveModel::Serializer
attribute :id
attribute :name
attribute :node
def node
serializer = ActiveModel::Serializer.serializer_for(object.node).new(object.node)
ActiveModel::Serializer::Adapter::Json.new(serializer).as_json
end
end |
I ran into this today, having previously used 0.9.x where nested associations were found. Is this 'expected behaviour' in that that's the way it is supposed to work, or would a PR be welcome? A small change gets this behaviour back - see my fork Current-RMS@80de719 |
@quaternion ans @groyoh said, this is indeed the expected behaviour. |
This one caught me doing an upgrade from 0.9 -> 0.10. The frustrating part is that I couldn't find any documentation about it, though perhaps I wasn't searching for the right terms. So I ended up spending a bit of time debugging why it was silently ignoring my I get the mindset behind guiding people towards a more lean implementation, however it seems that guidance would be better put into documentation and "how to" write-ups instead of a behavior change resulting in inconsistency. It seems like, unless you just plain don't want to support nested associations for "opinionated framework" reasons, offer the API and simply guide people away from using it. As for my opinion, I think it should be supported to use Hope that helps add a data point to your decision-making process. Thanks! |
Waiting for this PR: #952 and relevant issue: #835 (comment)) By the way, to add to @groyoh's comment (#921 (comment)), for class Post
attributes :comments
def comments
object
.comments
.map { |x| ActiveModel::Serializer::Adapter::FlattenJson.new(CommentSerializer.new(x)).as_json }
end
end |
Hey everyone there is actually a new PR in place as well, it's a different approach I have not review it yet, but it seems nice: #1073 |
Example 0.10.x:
PostSerializer produces json without a user node:
Is this a bug or a feature?
The text was updated successfully, but these errors were encountered: