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

belongs_to with custom foreign_key is breaking in 2.0.0.rc #3184

Closed
maokomioko opened this issue Aug 9, 2019 · 3 comments
Closed

belongs_to with custom foreign_key is breaking in 2.0.0.rc #3184

maokomioko opened this issue Aug 9, 2019 · 3 comments

Comments

@maokomioko
Copy link
Contributor

Having a model with indirect association:
belongs_to :organization, primary_key: :org_number, foreign_key: :org_number

Belongs to association dropdowns return object's ID (as primary key) instead of customly specified key.

This looks like a regression from here:
be7d2f4#diff-3ccbdf28331673e0d29522ba324bddcfR81

image

Just to be sure I switched back to the earlier commit and dropdown values are filled with org_number as expected.

@maokomioko maokomioko changed the title belongs_to with custom foreign_key is breaking in 2.0.0 r.c belongs_to with custom foreign_key is breaking in 2.0.0.rc Aug 9, 2019
@mshibuya
Copy link
Member

Belongs to association dropdowns return object's ID (as primary key) instead of customly specified key.

Yeah that's the intention of my fix. What are you trying to do? How does your schema look like and why do you need previous behavior?

@maokomioko
Copy link
Contributor Author

Here is a more detailed look.

class EditorOrganizationRole < OrganizationRole
end

class OrganizationRole < ApplicationRecord
  belongs_to :organization, primary_key: :org_number, foreign_key: :org_number
end

class Organization < ApplicationRecord
end
create_table "organizations". force: :cascade do |t|
  t.string "org_number"
  # other fields
end

I'm trying to either create or modify EditorOrganizationRole.

A dropdown for organizations in production (pre-patch version) returns the org_number as a key:
image

The same dropdown in the current version (after-patch) returns an ID instead:
image

And that leads to the validation error, because editor_organization_role#org_number is getting organization's ID and lookup fails.

image

@mshibuya
Copy link
Member

Sorry it was my fault, just pushed a fix 0e92ca4 in the master.
Thanks for reporting!

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

No branches or pull requests

2 participants