-
-
Notifications
You must be signed in to change notification settings - Fork 277
fix: belongs_to field should use association primary_key #3665
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
base: main
Are you sure you want to change the base?
Conversation
Code Climate has analyzed commit d7663e1 and detected 0 issues on this pull request. View more on Code Climate. |
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.
Thanks for looking into this, @Nevelito!
I tested this PR using the reproduction repo, and it does seem to resolve the issue. However, the test coverage doesn't currently reproduce the problem. Would it be possible to add a belongs_to
relationship that uses primary_key
in the dummy app and write a test against that scenario?
schema:
Do you have any ideas why it is happening ? I could not find any info in Internet. |
I pushed changes, have a look in free time. I will try to fix it tomorrow one more time. |
The Let's ensure that all the tests run successfully. |
Hi, I’m still having some issues with testing. Could you take a look and maybe suggest something I could try? |
Hei @Nevelito I suggest setting up the test scenario using the |
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.
Thanks for looking into this, @Nevelito !
Everything looks clean, and the setup appears to be correct. I’ve left some comments on the code, let’s continue the discussion there.
lib/avo/fields/belongs_to_field.rb
Outdated
# Quick fix for "Polymorphic associations do not support computing the class." | ||
rescue | ||
nil |
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.
This was a temporary fix for an error encountered during development. Could you remove this rescue and verify if the error still occurs? If it does, let's investigate the root cause and implement a more robust solution if possible. Thanks!
lib/avo/fields/belongs_to_field.rb
Outdated
record_id = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param).id | ||
found_record = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param) | ||
|
||
record_id = found_record&.send(primary_key.presence || :id) |
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.
record_id = found_record&.send(primary_key.presence || :id) | |
record_id = found_record.send(primary_key.presence || :id) |
This line must necessarily have a found_record
. If no record is found, it indicates an issue that needs to be addressed. Let's remove the safe navigator here, as well as on line 232, and check if anything breaks. If it does, we should investigate the root cause.
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.
Addressed here 912c9fd
spec/features/avo/belongs_to_spec.rb
Outdated
describe "with custom primary key set" do | ||
let(:event) { create(:event, name: "Sample Event") } | ||
|
||
let!(:volunteer) { create(:volunteer, event: event) } | ||
|
||
it "displays the event link using the UUID and stores the correct foreign key" do | ||
expect(volunteer.event_uuid).to eq(event.uuid) | ||
end | ||
end |
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.
This test is not effectively validating the changes. If we use the same setup on main
without the logic changes to the belongs_to
field, it still runs successfully without any failures.
Let's correctly set the volunteer with field :event, as: :belongs_to
and write a test that ensures a volunteer is created, during creation it selects an event, and is correctly associated with that event after creation.
This PR has been marked as stale because there was no activity for the past 15 days. |
Description
If record had primary_key set, it was still using :id
Changes:
Fixes # (issue)
Issue: #3413
Checklist:
Screenshots & recording