class InverseAssociationTests

Public Instance Methods

test_associations_with_no_inverse_of_should_return_nil() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 182
def test_associations_with_no_inverse_of_should_return_nil
  has_one_ref = Club.reflect_on_association(:sponsor)
  assert_nil has_one_ref.inverse_of

  has_many_ref = Club.reflect_on_association(:memberships)
  assert_nil has_many_ref.inverse_of

  belongs_to_ref = Sponsor.reflect_on_association(:sponsor_club)
  assert_nil belongs_to_ref.inverse_of
end
test_inverse_of_method_should_supply_the_actual_reflection_instance_it_is_the_inverse_of() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 171
def test_inverse_of_method_should_supply_the_actual_reflection_instance_it_is_the_inverse_of
  has_one_ref = Man.reflect_on_association(:face)
  assert_equal Face.reflect_on_association(:man), has_one_ref.inverse_of

  has_many_ref = Man.reflect_on_association(:interests)
  assert_equal Interest.reflect_on_association(:man), has_many_ref.inverse_of

  belongs_to_ref = Face.reflect_on_association(:man)
  assert_equal Man.reflect_on_association(:face), belongs_to_ref.inverse_of
end
test_should_allow_for_inverse_of_options_in_associations() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 137
def test_should_allow_for_inverse_of_options_in_associations
  assert_nothing_raised do
    Class.new(ActiveRecord::Base).has_many(:wheels, inverse_of: :car)
  end

  assert_nothing_raised do
    Class.new(ActiveRecord::Base).has_one(:engine, inverse_of: :car)
  end

  assert_nothing_raised do
    Class.new(ActiveRecord::Base).belongs_to(:car, inverse_of: :driver)
  end
end
test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 151
def test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse
  has_one_with_inverse_ref = Man.reflect_on_association(:face)
  assert has_one_with_inverse_ref.has_inverse?

  has_many_with_inverse_ref = Man.reflect_on_association(:interests)
  assert has_many_with_inverse_ref.has_inverse?

  belongs_to_with_inverse_ref = Face.reflect_on_association(:man)
  assert belongs_to_with_inverse_ref.has_inverse?

  has_one_without_inverse_ref = Club.reflect_on_association(:sponsor)
  assert !has_one_without_inverse_ref.has_inverse?

  has_many_without_inverse_ref = Club.reflect_on_association(:memberships)
  assert !has_many_without_inverse_ref.has_inverse?

  belongs_to_without_inverse_ref = Sponsor.reflect_on_association(:sponsor_club)
  assert !belongs_to_without_inverse_ref.has_inverse?
end
test_this_inverse_stuff() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 193
def test_this_inverse_stuff
  firm = Firm.create!(name: "Adequate Holdings")
  Project.create!(name: "Project 1", firm: firm)
  Developer.create!(name: "Gorbypuff", firm: firm)

  new_project = Project.last
  assert Project.reflect_on_association(:lead_developer).inverse_of.present?, "Expected inverse of to be present"
  assert new_project.lead_developer.present?, "Expected lead developer to be present on the project"
end