mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #39754 from stevenweber/s/34255-fix
Set inverse during has one autosave if necessary
This commit is contained in:
commit
1037f1cdf7
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 @@ module ActiveRecord
|
|||
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 @@ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue