class InverseBelongsToTests

Public Instance Methods

test_child_instance_should_be_shared_with_newly_built_parent() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 566
def test_child_instance_should_be_shared_with_newly_built_parent
  f = faces(:trusting)
  m = f.build_man(name: "Charles")
  assert_not_nil m.face
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to just-built-parent-owned instance"
end
test_child_instance_should_be_shared_with_newly_created_parent() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 577
def test_child_instance_should_be_shared_with_newly_created_parent
  f = faces(:trusting)
  m = f.create_man(name: "Charles")
  assert_not_nil m.face
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
end
test_child_instance_should_be_shared_with_parent_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 538
def test_child_instance_should_be_shared_with_parent_on_find
  f = faces(:trusting)
  m = f.man
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
end
test_child_instance_should_be_shared_with_replaced_via_accessor_parent() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 601
def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
  f = Face.first
  m = Man.new(name: "Charles")
  f.man = m
  assert_not_nil m.face
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
end
test_eager_loaded_child_instance_should_be_shared_with_parent_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 548
def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find
  f = Face.all.merge!(includes: :man, where: { description: "trusting" }).first
  m = f.man
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"

  f = Face.all.merge!(includes: :man, order: "men.id", where: { description: "trusting" }).first
  m = f.man
  assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  f.description = "gormless"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  m.face.description = "pleasing"
  assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
end
test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 588
def test_should_not_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"
  iz.topic = "Cow tipping"
  assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
end
test_trying_to_use_inverses_that_dont_exist_should_raise_an_error() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 613
def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
  assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_man }
end