-
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
Recurse associations by default #1205
Comments
You are right that the current default behavior of AMS with the Json/Attributes adapters is to embed only the first level associations. I am personally in favor of making the default embedding policy |
@beauby this workaround is quite handy, I vote to be a default :) |
I'd rather make deep nesting an intentional oractice, rather than easy, and so therefore like that more than two levels requires explicitness. |
@bf4 I don't feel strongly either way, but it should be made clear in the docs so that people don't get surprised by this behavior (intuitively, at least I, would expect all relationships to be serialized for the Json/Attributes adapters). |
I'm against having ** by default, as it could lead to an infinite loop of including, and redundant data. :-\ I think it's one step of magic that would cause a lot of confusion among many. |
@NullVoxPopuli so what do you think about associations explicity saying they need to be included? |
@NullVoxPopuli I agree this would happen, although it would just be the consequence of bad design (Json adapter + cyclic data graph). |
The trend right now is to only have the model you ask for, and the ids of the asnociations for that model. I know ember prefers things to be sideloaded, so, this would align with that. And there is a push for async relationships as well. But depending on what other people want to do, and what their workflow is, maybe we need a configuration option? I don't know what is 'best', but having an AMS option that makes you specify what associations you want every time could be pretty helpful. Of course, you can do it right now just by passing empty string to the include option. When serializing |
@NullVoxPopuli If you start doing sideloading and async relationships, you might as well go with JSON API. The goal of the Json adapter should not be to define a poor man's version of JSON API. |
Agreed On Sun, Oct 4, 2015, 10:30 AM Lucas Hosseini [email protected]
|
We should maybe say that in the docs though, so people don't try it On Sun, Oct 4, 2015, 10:31 AM L. Preston Sego III [email protected] wrote:
|
I think that specify what associations you want is pretty helpful too and when I looked to the code I saw that adding 3 lines we can do it!!! If I'm right (please correct me if I'm wrong) changing module ActiveModel
class Serializer
module Adapter
class CustomAttributes < Attributes
def serializable_hash_for_collection(options)
serializer.map { |s| CustomAttributes.new(s, instance_options).serializable_hash(options) }
end
def relationship_value_for(association, options)
return association.options[:virtual_value] if association.options[:virtual_value]
return unless association.serializer && association.serializer.object
include_tree_hash = @include_tree[association.key].instance_variable_get(:@hash)
include_association_hash = IncludeTree.from_include_args(association.options[:include]).instance_variable_get(:@hash)
include_tree = IncludeTree.from_include_args(include_association_hash.merge include_tree_hash)
opts = instance_options.merge(include: include_tree)
CustomAttributes.new(association.serializer, opts).serializable_hash(options)
end
end
end
end
end ... now I can use class UserSerializer < ApplicationSerializer
has_many :accounts, serializer: UserScope::AccountSerializer, include: :roles
end ps: the |
Many client-side applications are architected in a way that make it easy to retrieve additional resources as needed. Also, with HTTP/2 there is little network overhead. Thus, I'd vote for having it non-recursive by default (i.e. first-level only). |
I feel like at this point, everyone wants the json adapter to fit their specific use case -- it's not really capable of doing that. I think the json adapter should be an example for how to make a basic adapter that aids in rendering basic json documents, such that users can implement whatever they need as they please. JSON API is the future. |
Recurse was the default behavior in |
@prusswan You are right. Would you mind issuing a PR to update the docs? That would really help. |
There is some inconsistency when using |
I think recursing by default is fine because that was the behavior in 0.9.x (and 0.8.x afaik). What I would like though is a way to easily override the default serializer for associations via the adapter. In our case, we want to load only the IDs of the associated record unless explicitly included using the "include" parameter. Defaulting to one-level deep does not make sense for us. Unless I am missing something, this requires a lot of rewriting and hacks because the serializer is already determined when it reaches the adapter. I was hoping to be able to just check the include tree, override the |
So, the initial issue here is whether the Pros:
Cons:
The current consensus is that the default behavior will remain including only one level of relationships, although this should be explicitly stated in the docs. For other issues mentioned here, please open separate GH issues. Closing this issue for now, but feel free to keep commenting/making a case for it here. |
Given these two serializers
If I call
It will serialize
teams
, but notmembers
. Formembers
I would have to specifyinclude: {teams: :members}
option for it to work.But this seems very strange to me. Intuitively, by default it should go recursively.
The text was updated successfully, but these errors were encountered: