Skip to content

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
seako:fix-serialization-of-two-relationships-with-same-primary-key
Jul 20, 2016
Merged

use resource value instead of model attribute to get relationship model id for one to many relationships#746
lgebhardt merged 2 commits intoJSONAPI-Resources:masterfrom
seako:fix-serialization-of-two-relationships-with-same-primary-key

Conversation

@seako
Copy link
Copy Markdown
Contributor

@seako seako commented Jun 29, 2016

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.

Chris Kolodin 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"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prior to this patch this data contained the even posts and not the odd posts

@lgebhardt lgebhardt merged commit cdcd04c into JSONAPI-Resources:master Jul 20, 2016
@lgebhardt
Copy link
Copy Markdown
Contributor

@seako Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants