Skip to content

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

Open
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

Nevelito
Copy link
Contributor

@Nevelito Nevelito commented Feb 13, 2025

Description

If record had primary_key set, it was still using :id

Changes:

  • Change fill_field function in belongs_to_firld.rb
  • Add primary_key function
  • Add specs

Fixes # (issue)
Issue: #3413

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

Screenshots & recording

Copy link

codeclimate bot commented Feb 13, 2025

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

View more on Code Climate.

@Nevelito Nevelito changed the title fix: belongs_to field should use association primary_key [WIP] fix: belongs_to field should use association primary_key Feb 13, 2025
@Nevelito Nevelito changed the title [WIP] fix: belongs_to field should use association primary_key fix: belongs_to field should use association primary_key Feb 20, 2025
Copy link
Contributor

@Paul-Bob Paul-Bob left a 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?

@Nevelito
Copy link
Contributor Author

Nevelito commented Mar 5, 2025

FactoryBot.define do
  factory :user do
    uuid { SecureRandom.uuid }
    first_name { Faker::Name.first_name }
    last_name { Faker::Name.last_name }
    email { Faker::Internet.email }
    password { Faker::Internet.password }
    roles { {admin: false, manager: [true, false].sample, writer: [true, false].sample} }
    birthday { Faker::Date.birthday(min_age: 18, max_age: 65) }
    custom_css { ".header {\n  color: red;\n}" }
  end
  

schema:

create_table "users", force: :cascade do |t|
  t.string "email", default: "", null: false
  t.string "first_name"
  t.string "last_name"
  t.json "roles"
  t.date "birthday"
  t.text "custom_css"
  t.bigint "team_id"
  t.string "encrypted_password", default: "", null: false
  t.string "reset_password_token"
  t.datetime "reset_password_sent_at", precision: nil
  t.datetime "remember_created_at", precision: nil
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.boolean "active", default: true
  t.string "slug"
  t.string "uuid", null: false
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  t.index ["slug"], name: "index_users_on_slug", unique: true
  t.index ["team_id"], name: "index_users_on_team_id"
  t.index ["uuid"], name: "index_users_on_uuid", unique: true
end

image

Do you have any ideas why it is happening ? I could not find any info in Internet.

@Nevelito
Copy link
Contributor Author

Nevelito commented Mar 5, 2025

I pushed changes, have a look in free time. I will try to fix it tomorrow one more time.

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Mar 6, 2025

I pushed changes, have a look in free time. I will try to fix it tomorrow one more time.

The User and Post models are heavily used in many tests. If you notice that this change breaks existing tests, I suggest creating the test scenario with other models that are less burdened, such as Event, or even a new model if necessary.

Let's ensure that all the tests run successfully.

@Nevelito
Copy link
Contributor Author

Nevelito commented Mar 7, 2025

I pushed changes, have a look in free time. I will try to fix it tomorrow one more time.

The User and Post models are heavily used in many tests. If you notice that this change breaks existing tests, I suggest creating the test scenario with other models that are less burdened, such as Event, or even a new model if necessary.

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?

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Mar 7, 2025

Hei @Nevelito

I suggest setting up the test scenario using the Event model or another simpler model and set up a new association that uses a custom primary_key. If needed, you could even create a new model instead of dynamically adding columns and associations during tests. That should make things more manageable and reliable.

Copy link
Contributor

@Paul-Bob Paul-Bob left a 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.

Comment on lines 120 to 122
# Quick fix for "Polymorphic associations do not support computing the class."
rescue
nil
Copy link
Contributor

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!

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Addressed here 912c9fd

Comment on lines 162 to 170
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
Copy link
Contributor

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.

Copy link
Contributor

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

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

Successfully merging this pull request may close these issues.

2 participants