Skip to content

Commit

Permalink
Merge pull request #34533 from gmcgibbon/belongs_to_inverse_has_many
Browse files Browse the repository at this point in the history
Add support for belongs_to to has_many inversing.
  • Loading branch information
gmcgibbon committed Oct 2, 2019
2 parents 4ea7769 + d45c9ad commit b45699c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add support for `belongs_to` to `has_many` inversing.

*Gannon McGibbon*

* Allow length configuration for `has_secure_token` method. The minimum length
is set at 24 characters.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ def foreign_key_present?
owner._read_attribute(reflection.foreign_key)
end

# NOTE - for now, we're only supporting inverse setting from belongs_to back onto
# has_one associations.
def invertible_for?(record)
inverse = inverse_reflection_for(record)
inverse && inverse.has_one?
inverse_reflection_for(record)
end

def stale_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ def add_to_target(record, skip_callbacks = false, &block)
replace_on_target(record, index, skip_callbacks, &block)
end

def target=(record)
case record
when Array
super
else
add_to_target(record)
end
end

def scope
scope = super
scope.none! if null_scope?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,17 +607,17 @@ def test_child_instance_should_be_shared_with_newly_created_parent
assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
end

def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
def test_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
i = interests(:trainspotting)
m = i.man
assert_not_nil m.interests
iz = m.interests.detect { |_iz| _iz.id == i.id }
assert_not_nil iz
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
i.topic = "Eating cheese with a spoon"
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child"
iz.topic = "Cow tipping"
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance"
end

def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
Expand Down Expand Up @@ -704,17 +704,17 @@ def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed_with
assert_equal old_inversed_man.object_id, new_inversed_man.object_id
end

def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
def test_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
i = interests(:llama_wrangling)
m = i.polymorphic_man
assert_not_nil m.polymorphic_interests
iz = m.polymorphic_interests.detect { |_iz| _iz.id == i.id }
assert_not_nil iz
assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
i.topic = "Eating cheese with a spoon"
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child"
iz.topic = "Cow tipping"
assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance"
end

def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
Expand Down

0 comments on commit b45699c

Please sign in to comment.