class InverseHasOneTests

Public Instance Methods

test_parent_instance_should_be_shared_with_child_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 207
def test_parent_instance_should_be_shared_with_child_on_find
  m = men(:gordon)
  f = m.face
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
end
test_parent_instance_should_be_shared_with_eager_loaded_child_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 217
def test_parent_instance_should_be_shared_with_eager_loaded_child_on_find
  m = Man.all.merge!(where: { name: "Gordon" }, includes: :face).first
  f = m.face
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"

  m = Man.all.merge!(where: { name: "Gordon" }, includes: :face, order: "faces.id").first
  f = m.face
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
end
test_parent_instance_should_be_shared_with_newly_built_child() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 235
def test_parent_instance_should_be_shared_with_newly_built_child
  m = Man.first
  f = m.build_face(description: "haunted")
  assert_not_nil f.man
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
end
test_parent_instance_should_be_shared_with_newly_created_child() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 246
def test_parent_instance_should_be_shared_with_newly_created_child
  m = Man.first
  f = m.create_face(description: "haunted")
  assert_not_nil f.man
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 257
def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method
  m = Man.first
  f = m.create_face!(description: "haunted")
  assert_not_nil f.man
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
test_parent_instance_should_be_shared_with_replaced_via_accessor_child() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 268
def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
  m = Man.first
  f = Face.new(description: "haunted")
  m.face = f
  assert_not_nil f.man
  assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  m.name = "Bongo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  f.man.name = "Mungo"
  assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-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 280
def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
  assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.dirty_face }
end