-
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
Specify class when including relationships #75
Comments
Hi @KidA001 – the render jsonapi: Project.all,
class: { Project: API::V1::SerializableProject, Company: API::V1::SerializableCompany },
include: params[:include] If your serializers have a consistent naming scheme, you could override the module API
module V1
class ProjectsController < API::V1::ApplicationController
def jsonapi_class
Hash.new { |h, k| h[k] = "API::V1::Serializable#{k}".safe_constantize }
end
# GET /projects
def index
render jsonapi: Project.all,
include: params[:include]
end |
I think a cleaner approach would be to specify the class name inside serializer, so something like
That keeps the @beauby thoughts? |
I agree with @kapso, going with default serializer class for relationship by doing
would be a nice thing to add. |
@kapso @siepet If you look at the code history, it used to be that way. The reasons it was modified:
Note that in most use-cases, you would use the controller-level hooks to provide a static or dynamic hash, i.e. def jsonapi_class
@jsonapi_classes ||= {
# ...
}
end |
And as mentioned, if you have a consistent way of deriving the serializer name from the class name (which you should probably have), you can use a dynamic hash to lazily generate the mapping. |
@beauby yea I ended up using a controller method as well in my |
My code relied exactly on what you called "inconsistencies" to produce tweaks in the serialization that best fit my needs I have an appointment booking website where users can only access the phone number of people they have an appointments with. Assume I am rendering a user with his contacts, and I want to unlock the phone number for the users they are in contact with through appointments
I was taking advantage of a specific "serializer routing"
The phone number would be serialized for those users that have an appointment with the user, since I would declare a different serializer in the ConversationSerializer
Now I agree this is a bit of a (dirty hack), since we have no way of deciding which serializer to actually use (not sure where this is a first hit first serialize or something else), but it did serve me perfectly on this one. Not sure how to reproduce something similar on the new jsonapi-rb 0.3+. Or maybe something using decorators ? sounds like painful... But if you have an idea / suggestion and it works, I'd migrate right away to jsonapi-rails 3.x This is basically what prevent me from upgrading to jsonapi_compliable 0.11 of @richmolj that upgraded jsonapi-rails 0.2.x to 0.3.x
killed it once and for all (maybe not for the "how it was chosen" part, but this is mainly the library user's job I would say) |
In my controller I have
In my Serializer I have
When I pass the
include
params withGET /projects?include=companies
, I getundefined method 'new' for nil:NilClass
. I'm assuming because it can't findAPI::V1::SerializableCompany
How am I supposed to specify the class for all includes? This was a simplified example, but some of my Models have multiple relationships.
The text was updated successfully, but these errors were encountered: