-
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
Dynamic serializer selection #1719
Comments
cant you just do this as an attribute then and make a method for said attribute?
I think if the method generates a model, that models serializer should spawn? |
@cgmckeever true, but I want to specify the serializer of the thing that spawned. For my particular use case, I am have a model called "Notification" It has a The default model's serializer is often too big for what I want. So I want to the source_obj to have use special "sparse" serializers for each object. e.g. sparse_user_serializer, sparse_profile_serializer, etc. etc. instead of the default model serializer of user_serializer or profile_serializer. does that make sense? I think my hack to fix this will be to do:
so I am at least not accidently getting any N+1s |
@KevinColemanInc you can use this: class MySerializer < ActiveModel::Serializer
def self.serializer_for(model, options)
return SparseAdminSerializer if model.class == 'Admin'
super
end
# the rest of the serializer
end At the moment there is no better solution available. |
@groyoh NICE! |
Cool! I will use that. |
We should document this. PR anyone? |
Currently the serializer param on
has_x
takes an object.It would be cool if it could accept a method name as well.
Use case:
Dynamically setting the serializer for polymorphic relationships:
The text was updated successfully, but these errors were encountered: