close
Skip to content

Use correct key in unconventional associations#2292

Merged
pablobm merged 4 commits into
thoughtbot:mainfrom
pablobm:associated-primary-key
Apr 6, 2023
Merged

Use correct key in unconventional associations#2292
pablobm merged 4 commits into
thoughtbot:mainfrom
pablobm:associated-primary-key

Conversation

@pablobm
Copy link
Copy Markdown
Collaborator

@pablobm pablobm commented Nov 29, 2022

A fix for a bug that you can reproduce right now easily: visit the demo app (https://administrate-prototype.herokuapp.com/admin/customers), select any customer, edit, save, and the customer will lose their territory value.

This is a bug introduced with #1633, where I failed to use associated_primary_key to refer to the key on the other side of a foreign key.

Specifically in our demo app, the Customer model belongs to Country . However instead of the conventional matching of Customer#country_id to Country#id, it should match Customer#country_code to Country#code. While foreign_key gives us country_code, we wrongly use primary_key for the counterpart. Instead we should use association_primary_key which gives us code.

Fixes #2169

@pablobm pablobm force-pushed the associated-primary-key branch 4 times, most recently from 59e82d0 to fdcf67a Compare November 29, 2022 10:42
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.

POST to create through controller with different FK constraint than 'id' fails

1 participant