-
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
[WIP] fix belongs_to causing unnecessary db hits #1750
Closed
NullVoxPopuli
wants to merge
1
commit into
rails-api:master
from
NullVoxPopuli:fix-unneeded-db-hits-for-belongs-to
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,21 @@ def test_associations_custom_keys | |
assert expected_association_keys.include? :site | ||
end | ||
|
||
class BelongsToTestPostSerializer < ActiveModel::Serializer | ||
belongs_to :blog | ||
end | ||
|
||
test 'belongs_to should not load record' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test courtesy of @ssendev |
||
post = Post.new | ||
post.blog_id = 'blog' | ||
post.blog = 'should not touch this' | ||
|
||
actual = serializable(post, adapter: :json_api, serializer: BelongsToTestPostSerializer).as_json | ||
expected = { data: { id: 'post', type: 'posts', relationships: { blog: { data: { id: 'blog', type: 'blogs' } } } } } | ||
|
||
assert_equal expected, actual | ||
end | ||
|
||
class InlineAssociationTestPostSerializer < ActiveModel::Serializer | ||
has_many :comments | ||
has_many :comments, key: :last_comments do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in JSONAPI::Resources, the relationship object has a
foreign_key_on
option that may beself
.docs
see https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/relationship.rb for full code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I'm just trying to get a handle on how all this existing reflection code works.
@bf4, can you look over this? I want to make sure I know what's happening.
It looks like the most problematic area is in reflection.rb#build_association
if I understand things correctly, it's really just the
value
method and how the association/reflection is represented that needs to change?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NullVoxPopuli I know @bf4 knows this area better than I do, but wanted to address what I think one key problematic area is, and how it relates to #1720:
The issue I see here is sometimes you want a
link
, notid/type
combo. In this case:#value
to A) see if we wantid/type
in the response (include_data
) and B) get the actual links data.In code:
Unfortunately I think this would cause the same inline-association issue. Just for the sake of argument, let's say the solution was a
type
line, you'd have something like this:In order to get the
type
value, you need to run the block, which is going to fire the unnecessary SQL again.This is the why #1720 changes inline-associations to be lazily evaluated by wrapping the data loading in a proc: