Skip to content
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

Fix exception when using a resource with a composite primary/foreign key association #3335

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

filipegiusti
Copy link
Contributor

Fixes #3334

Fix NoMethodError: undefined method to_sym' for an instance of Array` raised by this line when an association uses a composite primary key/foreign key.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

Fix avo-hq#3334

Fix `NoMethodError: undefined method `to_sym' for an instance of Array` raised by this line when an association uses a composite primary key/foreign key.
Copy link

codeclimate bot commented Oct 11, 2024

Code Climate has analyzed commit 0a745ed and detected 0 issues on this pull request.

View more on Code Climate.

@adrianthedev
Copy link
Collaborator

Thanks @filipegiusti!
Would you be able to add some tests for this as well?

The approach I would take is to create a new model & resource which uses composite foreign keys and test that.

Thank you for tackling this!

@filipegiusti
Copy link
Contributor Author

filipegiusti commented Oct 21, 2024

The approach I would take is to create a new model & resource which uses composite foreign keys and test that.

I searched for a test file at spec/lib/avo/fields/belongs_to_field.rb or using the search term spec fields belongs_to but I couldn't find anything. I'm not sure where such test would be and how it would look like, I don't have much experience testing libraries.

PS: I also searched for some sort of coverage tool that would help pinpoint which tests cover that method and tried to get github copilot to find such tests, all without luck.

@adrianthedev
Copy link
Collaborator

Can you create a new spec next to this one and test only the composite behavior?

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Nov 4, 2024

@filipegiusti I can take over and create the test. Could you share some details about your environment setup? We don’t have any tables with composite primary or foreign keys, so I’m unsure how to configure the environment to test this change.

@Paul-Bob Paul-Bob marked this pull request as draft November 5, 2024 09:10
@filipegiusti
Copy link
Contributor Author

It all started when we added the primary_key and foreign_key. We have these models.

class Profile
  has_one :membership, primary_key: %i[organization_id id], foreign_key: %i[organization_id profile_id], dependent: :destroy, inverse_of: :profile
end

class Membership
  belongs_to :profile, primary_key: %i[organization_id id], foreign_key: %i[organization_id profile_id], inverse_of: :membership
end

Both models have a primary key of id and an unique index of [organization_id, id].

The edit page of Profile works, the exception is raised when Profile is attempted to be updated.

We also have:

class Resources::Profile < Avo::BaseResource
  def fields
    field :membership, as: :belongs_to, only_on: :show
  end
end

@filipegiusti
Copy link
Contributor Author

I might be able to look at this next week.

Copy link
Contributor

This PR has been marked as stale because there was no activity for the past 15 days.

@github-actions github-actions bot added the Stale label Nov 21, 2024
@Paul-Bob
Copy link
Contributor

I might be able to look at this next week.

That would be great,

Thank you!

@github-actions github-actions bot removed the Stale label Nov 22, 2024
Copy link
Contributor

github-actions bot commented Dec 7, 2024

This PR has been marked as stale because there was no activity for the past 15 days.

@github-actions github-actions bot added the Stale label Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception is raised when association uses a composite primary/foreign key
3 participants