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

Foreign key change doesn't reset the association #43245

Closed
heaven opened this issue Sep 17, 2021 · 4 comments
Closed

Foreign key change doesn't reset the association #43245

heaven opened this issue Sep 17, 2021 · 4 comments

Comments

@heaven
Copy link

heaven commented Sep 17, 2021

When changing the foreign key, the association is being reloaded as expected

2.6.6 :099 > admin = Admin.first
  Admin Load (0.5ms)  SELECT ...
 => #<Admin id: 1>
2.6.6 :100 > admin.saas_company
  SaasCompany Load (0.5ms)  SELECT ...
 => #<SaasCompany id: 2, name: "Company #2">
2.6.6 :101 > admin.saas_company_id = 3
 => 3
2.6.6 :102 > admin.saas_company
  SaasCompany Load (0.6ms)  SELECT ...
 => #<SaasCompany id: 3, name: "Company #3">

But this doesn't happen when running validations while updating a record. Here's the validation in the Admin model

validates :saas_company, inclusion: { in: proc { |admin|
  debugger
  admin.saas_companies
} }, allow_nil: true

And here's what I see in the debugger

(byebug) admin.saas_company
 => #<SaasCompany id: 1>
(byebug) admin.saas_company_id
 => 3
(byebug) admin.changes
 => {"saas_company_id"=>[1, 3]}

Changing the saas_company to saas_company_id in the validation works but causes other problems.

Rails 6.1.3.2

@heaven
Copy link
Author

heaven commented Sep 17, 2021

Changing this in the Admin model

belongs_to :saas_company, inverse_of: :current_admins

To this

belongs_to :saas_company

Fixes the problem. So it seems something is wrong with the inverse option. I think this is not expected behavior.

@ghiculescu
Copy link
Member

@heaven any chance you could try and replicate the issue against Rails 7 alpha? It sounds similar to #43222

@heaven
Copy link
Author

heaven commented Sep 18, 2021

@ghiculescu wasn't able to upgrade to 7 but can try setting up a test example similar to #43222

@rails-bot
Copy link

rails-bot bot commented Dec 17, 2021

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Dec 17, 2021
@rails-bot rails-bot bot closed this as completed Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants