I’ve found a regression in Rails 6 while upgrading my Rails 5.2 app that I’m sure is not correct behavior but I’d like a core member to confirm or not before I create a PR for it. The root of the problem lies in the association cache in the internals of ActiveRecord. The example below is the root of a more complex issue in my app, but the following is as basic as I can get to show the issue.
class Customer has_many :appointments end class Appointment belongs_to :customer end
Example that works in Rails 5.2
a = Appointment.new customer_id: @customer.id a.association(:customer).reader # returns @customer
Example that doesn’t work in Rails 6.0
a = Appointment.new customer_id: @customer.id a.association(:customer).reader # returns nil
Example that works in both Rails 5.2 and 6.0 (note the association is set without the id)
a = Appointment.new customer: @customer a.association(:customer).reader # returns @customer
So is the example that doesn’t work in Rails 6.0 a bug? The example I provided isn’t something you’ll see in a Rails app, but Rails internals uses the association cache and if this is a bug, then fixing this would fix other potential issues. Again, I found this issue due to a regression in my code with autosaved associations that I was setting via a foreign key parameter (ie. customer_id).