-
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
Explicitly set serializer for associations #696
Conversation
if resource.respond_to?(:to_ary) | ||
config.array_serializer | ||
else | ||
get_serializer_for(resource.class) | ||
options.fetch(:options, {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to fetch :options
from :options
don't contribute to readability. But it's okay for me. It's just a detail, I liked the implementation 👍
If you want to try a different approach you could simply add options[:options]
content to the type
level.
@ggordon, would be cool to update the README either 😄 |
@joaomdmoura Thanks for feedback, I'll update the README. |
d3d72b0
to
7b683b8
Compare
I noticed the implementation doesn't guard against nil associations like the implicit version of the call, that is: class UserSerializer < ActiveModel::Serializer
# ...
end
# Assume comment.user == nil
class CommentSerializer < ActiveModel::Serializer
# This call guards against nil, outputting null, when implicitly using UserSerializer
belongs_to :user
# When it's made explicit, if user is nil, a UserSerializer is created anyway,
# with the nil object, and errors out trying to access members.
belongs_to :user, serializer: UserSerializer
end |
@mieko Thanks, I'll look into it. |
7b683b8
to
03829e1
Compare
@ggordon can you rebase? Also, can you find a better name for the |
@kurko I'll fix and rebase in the morning. |
Document specifying serializer for assocaition
03829e1
to
9f97158
Compare
Explicitly set serializer for associations
Thanks for your work, @ggordon, and sorry for the huuuuge delay. |
No problem. @kurko, thanks for merging! |
Adding back the ability to specify the serializer for an association.
Ex. has_one :author, serializer: PersonSerializer