Set inverse during has one autosave if necessary

pair: @rebeldroid12
This commit is contained in:
Steve Weber 2020-06-29 15:28:20 -05:00 committed by Eugene Kenny
parent 9509bc23c2
commit ba99e6d400
5 changed files with 20 additions and 1 deletions

@ -1,3 +1,9 @@
* Allow the inverse of a `has_one` association that was previously autosaved to be loaded.
Fixes #34255.
*Steven Weber*
* Optimise the length of index names for polymorphic references by using the reference name rather than the type and id column names.
Because the default behaviour when adding an index with multiple columns is to use all column names in the index name, this could frequently lead to overly long index names for polymorphic references which would fail the migration if it exceeded the database limit.

@ -463,7 +463,7 @@ def save_has_one_association(reflection)
unless reflection.through_reflection
record[reflection.foreign_key] = key
if inverse_reflection = reflection.inverse_of
record.association(inverse_reflection.name).loaded!
record.association(inverse_reflection.name).inversed_from(self)
end
end

@ -781,6 +781,17 @@ def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed_with
assert_same old_inversed_human, new_inversed_human
end
def test_inversed_instance_should_load_after_autosave_if_it_is_not_already_loaded
human = Human.create!
human.create_autosave_face!
human.autosave_face.reload # clear cached load of autosave_human
human.autosave_face.description = "new description"
human.save!
assert_not_nil human.autosave_face.autosave_human
end
def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
interest = interests(:llama_wrangling)
human = interest.polymorphic_human

@ -2,6 +2,7 @@
class Face < ActiveRecord::Base
belongs_to :human, inverse_of: :face
belongs_to :autosave_human, class_name: "Human", foreign_key: :human_id, inverse_of: :autosave_face
belongs_to :super_human, polymorphic: true
belongs_to :polymorphic_human, polymorphic: true, inverse_of: :polymorphic_face
# Oracle identifier length is limited to 30 bytes or less, `polymorphic` renamed `poly`

@ -4,6 +4,7 @@ class Human < ActiveRecord::Base
self.table_name = "humans"
has_one :face, inverse_of: :human
has_one :autosave_face, class_name: "Face", autosave: true, foreign_key: :human_id, inverse_of: :autosave_human
has_one :polymorphic_face, class_name: "Face", as: :polymorphic_human, inverse_of: :polymorphic_human
has_one :polymorphic_face_without_inverse, class_name: "Face", as: :poly_human_without_inverse
has_many :interests, inverse_of: :human