use resource value instead of model attribute to get relationship model id for one to many relationships#746
Merged
lgebhardt merged 2 commits intoJSONAPI-Resources:masterfrom Jul 20, 2016
Conversation
added 2 commits
June 29, 2016 12:34
when multiple relationships share the same foreign key and they are both
`included` the relationships are correctly loaded from the DB but
when serialized to json the records for the first defined relationship
are used to populate all the others.
for example:
given the following
class Thing < ActiveRecord::Base
belongs_to :example
end
class Example < ActiveRecord::Base
has_many :things
has_many :cool_things, -> { where(cool: true) },
foreign_key: :example_id, class_name: 'Thing'
has_many :uncool_things, -> { where(cool: false) },
foreign_key: :example_id, class_name: 'Thing'
end
class ExampleResource < JSONAPI::Resource
has_many :cool_things, foreign_key: :example_id,
relation_name: :cool_things, class_name: 'Thing
has_many :uncool_things, foreign_key: :example_id,
relation_name: :cool_things, class_name: 'Thing'
end
when a request is made for an example `include`ing its `cool_things` and
its `uncool_things` the `cool_things` records will be used to populate
the `uncool_things` as well.
this commit contains a test demonstrating this issue.
instead of fetching records using the foreign_key use the relationship name instead. the value stored in the map under the foreign key contains the records for the first relationship defined using that foreign key. by contrast using the relationship name returns the records fetched for the relationship defined with that name.
| data:[ | ||
| { | ||
| type: "posts", | ||
| id: "1" |
Contributor
Author
There was a problem hiding this comment.
prior to this patch this data contained the even posts and not the odd posts
Contributor
|
@seako Thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Instead of fetching records using the foreign_key use the relationship name instead. The value stored in the map under the foreign key contains the records for the first relationship defined using that foreign
key. By contrast, using the relationship name returns the records fetched for the relationship defined with that name. I have included a test case that fails without this patch that illustrates how I discovered this issue.
This is analogous to the recently merged pull (https://github.com/cerebris/jsonapi-resources/pull/736) that does the same thing for to_one relationships.