Closed dmke closed 1 year ago
Turns out, there must be something else borked... I've build a reproduction, which does what it should on all Rails versions so far (5.2, 6.0, 6.1).
Sorry for the noise.
No worries - glad you figured it out!
In the end, there was a misconfiguration: We have an Inquirable
module mixed which (a) got mixed in the *Inquiry
classes, and (b) dynamically creates the association based on the name of the class it was mixed in (e.g. HouseInquiry → belongs_to :house
, has_one :house_owner_billing
etc.).
The actual problem was a double declaration of the house_owner_billing
(for whatever reason; once in the mixin, once in the class itself). This didn't bother Rails < 6.1, but since 6.1 seems to be an error.
I'm in the process of updating an older Rails app from 5.2 to 7.0, and I stumbled upon a small(?) issue.
I'm currently on Rails 6.1, CPK 13.0.7, with a PostgreSQL database.
For context, this is a simplified view the current database layout (the interesting bits are highlighted):
These are the associations:
As you can see, the Ruby code tries to establish a strong connection between
HouseOwnerBilling
andHouseInquiry
by (ab)using the fact that they share the sameinquiry_id
. In the past, this has worked well:The upgrade to Rails 6.1 has now revealed a change:
I've found
ActiveRecord::Associations::HasOneAssociation#replace
to be the culprit (called bybuild_house_owner_association(options)
→association("house_owner_billing").build(options)
→set_new_record(record)
→replace(record, false)
), which does reset the owner:and in
ActiveRecord::Associations::ForeignAssociation#set_owner_attributes
:reflection.join_foreign_key
is["inquiry_id", "villa_id"]
, e.g.key == [42, 23]
reflection.join_primary_key == "inquiry_id"
record.inquiry_id = [owner.inquiry_id, owner.villa_id]
, which gets rejected (I suspect somewhere in Rails' typecasting mechanism)My current workaround is to "enhance"
build_house_owner_billing
, but that feels wrong:Do you have any suggestions as how to proceed from here?