active_model_serializers
active_model_serializers copied to clipboard
Add docs for `include` in Json adapter.
The include option with Json adapter works the same as with the JsonApi adapter, but it's not written anywhere in the docs.
That's interesting! Do you know if fields or something relevant is also working with JSON adapter? If not is there any interest to send a pull request for that ? Cause I really need it and I am going to implement it anyway..
@vasilakisfil Currently you can use the fields option with Json/Attributes adapters, but you can only filter the primary resources (render json: @posts, fields: 'id, title, body').
I am not sure extending the Json adapter to fit the broader JSON API style fields option (where you specify the attributes for each type) is a good thing. The goal is to keep Json/Attributes adapters very simple.
given that's an adapter I don't think it's an issue. Plus there should be an adapter that opts for compatibility with 0.9.x and 0.8.x (supporting only, except etc options). Otherwise the way to go is create an external plugin adapter just like ember? is there support for that? On Oct 6, 2015 5:03 PM, "Lucas Hosseini" [email protected] wrote:
I am not sure extending the Json adapter to fit the broader JSON API style fields option (where you specify the attributes for each type) is a good thing. The goal is to keep Json/Attributes adapters very simple.
— Reply to this email directly or view it on GitHub https://github.com/rails-api/active_model_serializers/issues/1243#issuecomment-145886808 .
Oh yeah, AMS is deisgned in such a way that you can create your own adapter and/or extend an existing one. I agree the migration from 0.8/0.9 should be made as easy as possible, I'm just not sure what's the best way to make that happen.
maybe for the initial 0.10 release, we could copy the old adapters? maybe name them json_08, json_09 etc
and then in the next release after 0.10, remove them?
First we have to make sure it is actually possible to build such legacy adapters.
@beauby, is there any reason to use the option include when rendering an object? My point is, if that option must be included when of rendering - render json: @posts, include: 'comments' e.g. -, I expect the serializer to exclude the comments relationship when used without the include option.
I have a use case where a user can have many sessions, in order to be able to sign them all out in case something bad happens. I have something like this:
class SessionsController
# ...
render json: @session, include: 'user'
# ...
end
class SessionSerializer
# ...
# I am not asking for the "UserSerializer" to include the "sessions" relationship.
# Nevertheless, it does. The "UserSerializer", for its own logical reasons, also
# declares its own relationship with "sessions" and other models.
# I should explicitly use the "include" option here if I wanted the other relationships
# to be included.
has_one :user, serializer: UserSerializer # include: ['other', 'relationships']
# ...
end
In my use case, I don't want the relationships of the user model to be included, but they do.
@phcoliveira Could you open a separate issue with the definition of your serializers and your render calls and a description of the unexpected behavior?
The include option does not seem to work for me w/the JSON adapter in RC3 at least. I cannot get it to include associations 2 levels down; just the top level seems to get included. Take the following for example:
admin -< businesses - industry
The business objects get nested appropriately in an array under admin but they do not include their industry. The key is not even present at all.
@bdmac Did you specify the include: '**' option on your render call?
Yes I tried that and also the more specific flavored of include.
— Sent from Mailbox
On Mon, Oct 19, 2015 at 9:25 AM, Lucas Hosseini [email protected] wrote:
@bdmac Did you specify the
include: '**'option on yourrendercall?Reply to this email directly or view it on GitHub: https://github.com/rails-api/active_model_serializers/issues/1243#issuecomment-149268449
@bdmac Could you open a separate issue in which you provide your models, serializers and render calls?
@beauby This is a great issue. As the domain master of includes, I'd love if you added some docs of how it behaves and what data structures are inputs and outputs.
I'm seeing what might be the same issue. I can't really get any use out of the include feature at all for the problem I'm trying to solve. I'm using the simple JSON adapter.
I have an Organization:
class OrganizationSerializer < ActiveModel::Serializer
attributes :id, :name, :created_at
has_many :apps, class_name: 'Client::Application', serializer: ClientAppSerializer
has_many :services
end
and a ClientAppSerializer:
class ClientAppSerializer < ActiveModel::Serializer
attributes :id, :name, :created_at
belongs_to :organization
end
and a ServiceSerializer:
class ServiceSerializer < ActiveModel::Serializer
attributes :id, :name, :created_at, :updated_at
belongs_to :organization
end
Pretty simple, right?
Let's say I want to serialize starting at the App level:
render json: @app, serializer: ClientAppSerializer
I'll get the serialized data for the App, and the serialized data for the Organization, but the Organization won't have any Services array.
So I try this...
render json: @app, serializer: ClientAppSerializer, include: 'organization.services'
... but nothing changes in my serialized output. No services.
Am I missing something fundamental here?
@mepatterson Could you open a separate issue adding the current output you're getting?
Done. https://github.com/rails-api/active_model_serializers/issues/1421
Hello, guys.
Can you tell me, please, whether it's possible to use this option inside the serializer class itself
class FooSerializer < ActiveModel::Serializer
attributes :foo, :bar, :baz
# something like this
include: '*'
end
rather than inside controller?
@d3crypt3d short answer is no. It's an adapter option. Please open a new issue to make a proposal if you'd like to discuss further, or better, join us on the slack chat. See https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md
@bf4 sorry for bothering, but is it issue relevant either? Quick check showed that include option is not working anymore for JSON adapter
@charlie-wasp I had thought it did... we have tests... but it's been a while since I worked on that area...
@bf4 sorry, my bad, I misunderstood how include option works. Yes, include works with the JSON adapter and even with the attributes adapter. I tested it on the gem version 0.10.2. However in docs include option is mentioned only in the JSON API section
@charlie-wasp Would love a PR to improve :)