-
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
Performance improvement with associations using includes? #1044
Comments
I really think AMS should not be responsible for any of the data-related stuff. However, I think the kind of queries that would implicitly be fired by AMS through ActiveRecord should be documented, as well as suggestions and/or helper methods to help preload stuff with AR. |
@beauby +1 |
@beauby I agree up to a point. I'm looking at this from the perspective of what is the most common use case as well as what does the user interface look like. As it currently stands, the
Doing it in the controller is messy and a ton of boilerplate that should be avoided (eg having to specify associations twice). If instead we had an option ( If it's specified in the serializer, it limits whether it can be customized per controller action. The serializer has all the information it needs to determine whether an Personally I think that at some point there's going to need to be a |
Perhaps provide a hook of some sort where you can 'prepare' an object for an association? |
@bf4 yeah that would work, but still has the problem of being opt-in. Whereas if you're serializing an AR object with associations included (in the serialized hash), I can't think of a single time where those associations should not be included. |
Polymorphic handlng of poros vs arecord? |
@bf4 I think that would be the right approach yeah. :) |
I'd just hope we remain conservative in what we promise and easy to customize, so as to keep maintenance needs low, mostly at the interface level, and minimize bugs... Expanding the scope to manage AR associations worries me. |
Ooh! The poro is activemodel::serializer and the ar is activerecord::serializer!!! |
@bf4 that's what I was outlining above yeah. :) if it seems like that's the best option I think we go for it. ❤️ |
I think there should be a helper or a separate gem in order to fetch "just the right data" depending on a serializer and serialization options. Thoughts @joaomdmoura @bf4 @NullVoxPopuli? |
would a reasonable api be just to document the following?:
I think we could just I was going to add a method to the serializer, but realized this might be the cleanest way, though not the most intuitive, and it's still declarative and explicit. |
I have some endpoints where the client can pass in various AMS I think a separate helper/Gem that parses the AMS string (i.e. Does AMS internally expand the string form to a simple nested hash? Perhaps exposing that publicly could help here. |
does |
@NullVoxPopuli That looks exactly like what I need. Thanks! |
woo! |
Closing this as AMS will not do auto-including at ActiveRecord level. Moreover, one can do includes = ActiveModel::Serializer::IncludeTree::Parsing.include_string_to_hash(include_string)
# Or
# includes = JSON::API::IncludeDirective.new(include_string).to_hash
# if using the `jsonapi_parser` gem.
render json: Post.includes(includes) |
oh hey, that's a good idea. |
@beauby That solution looks interesting, what would the format of |
@ram535ii The format would have to be as described by the JSON API spec:
An other option is to use the more format-permissive |
For new versions (after #1685 Replace IncludeTree with IncludeDirective from the jsonapi gem.): JSONAPI::IncludeDirective.new(params[:include], allow_wildcard: true).to_hash
# In: <string>"tags,author.user"
# Out: <hash> {:tags=>{}, :author=>{:user=>{}}} |
Just wanted to throw this in here for someone with more knowledge about AMS than I.
It seems as though it would be a prudent thing to do to check to see if the thing being serialized in an
ActiveRecord::Relation
and, if so, fire off anincludes
for the set of associations on the serializer.This doesn't seem to me to be something that should be handled at a higher level since we're specifying the associations we want in the serializer. Doing it a level up just means tons of duplication.
I've noticed AMS being the cause of hundreds of queries being fired when it should just be making dozens. It seems to be causing a ton of timeouts for us.
So if we had:
Then AMS would attach
includes(:comments, :author)
before it fires the method that causes the query to be executed on the DB.Thoughts?
The text was updated successfully, but these errors were encountered: