Skip to content

Lookup polymorphic type from the column before fetching the record#1326

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:release-0-9from
st0012:improve-type-for-source
Jan 25, 2021
Merged

Lookup polymorphic type from the column before fetching the record#1326
lgebhardt merged 1 commit intoJSONAPI-Resources:release-0-9from
st0012:improve-type-for-source

Conversation

@st0012
Copy link
Copy Markdown
Contributor

@st0012 st0012 commented Aug 23, 2020

Currently, Relationship#type_for_source looks up associated record's type by:

  1. Fetching its associated record.
  2. Calling .class._type on that record.

This approach is inefficient because usually, a polymorphic record (the source) has _type and _id columns. And the _type column's value will be the polymorphic relationship record's type.

So this PR makes type_for_source check the type value from column before fetching the associated record.

All Submissions:

  • I've checked to ensure there aren't other open Pull Requests for the same update/change.
  • I've submitted a ticket for my issue if one did not already exist.
  • My submission passes all tests. (Please run the full test suite locally to cut down on noise from travis failures.)
  • I've used Github auto-closing keywords in the commit message or the description.
  • I've added/updated tests for this change.

New Feature Submissions:

  • I've submitted an issue that describes this feature, and received the go ahead from the maintainers.
  • My submission includes new tests.
  • My submission maintains compliance with JSON:API.

Bug fixes and Changes to Core Features:

  • I've included an explanation of what the changes do and why I'd like you to include them.
  • I've provided test(s) that fails without the change.

Test Plan:

Reviewer Checklist:

  • Maintains compliance with JSON:API
  • Adequate test coverage exists to prevent regressions

Currently, `Relationship#type_for_source` looks up polymorphic source's
type by:

1. Fetching its associated record.
2. Calling `.class._type` on that record.

This approach is inefficient because usually, a polymorphic record (the
`source`) has
`_type` and `_id` columns. And the `_type` column's value will be the
polymorphic relationship record's type.

So this PR makes `type_for_source` check the type value from column
before fetching the associated record.
@st0012 st0012 changed the title Lookup polymorphic type from the column before the record Lookup polymorphic type from the column before fetching the record Aug 23, 2020
@lgebhardt lgebhardt merged commit 20e051c into JSONAPI-Resources:release-0-9 Jan 25, 2021
@lgebhardt
Copy link
Copy Markdown
Contributor

@st0012 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