class AssociationsTest

Public Instance Methods

test_association_with_references() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 109
def test_association_with_references
  firm = companies(:first_firm)
  assert_includes firm.association_with_references.references_values, "foo"
end
test_bad_collection_keys() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 66
def test_bad_collection_keys
  assert_raise(ArgumentError, "ActiveRecord should have barked on bad collection keys") do
    Class.new(ActiveRecord::Base).has_many(:wheels, name: "wheels")
  end
end
test_eager_loading_should_not_change_count_of_children() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 29
def test_eager_loading_should_not_change_count_of_children
  liquid = Liquid.create(name: "salty")
  molecule = liquid.molecules.create(name: "molecule_1")
  molecule.electrons.create(name: "electron_1")
  molecule.electrons.create(name: "electron_2")

  liquids = Liquid.includes(molecules: :electrons).references(:molecules).where("molecules.id is not null")
  assert_equal 1, liquids[0].molecules.length
end
test_force_reload() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 80
def test_force_reload
  firm = Firm.new("name" => "A New Firm, Inc")
  firm.save
  firm.clients.each {} # forcing to load all clients
  assert firm.clients.empty?, "New firm shouldn't have client objects"
  assert_equal 0, firm.clients.size, "New firm should have 0 clients"

  client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
  client.save

  assert firm.clients.empty?, "New firm should have cached no client objects"
  assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"

  firm.clients.reload

  assert !firm.clients.empty?, "New firm should have reloaded client objects"
  assert_equal 1, firm.clients.size, "New firm should have reloaded clients count"
end
test_include_with_order_works() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 61
def test_include_with_order_works
  assert_nothing_raised { Account.all.merge!(order: "id", includes: :firm).first }
  assert_nothing_raised { Account.all.merge!(order: :id, includes: :firm).first }
end
test_loading_the_association_target_should_keep_child_records_marked_for_destruction() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 46
def test_loading_the_association_target_should_keep_child_records_marked_for_destruction
  ship = Ship.create!(name: "The good ship Dollypop")
  part = ship.parts.create!(name: "Mast")
  part.mark_for_destruction
  assert ship.parts[0].marked_for_destruction?
end
test_loading_the_association_target_should_load_most_recent_attributes_for_child_records_marked_for_destruction() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 53
def test_loading_the_association_target_should_load_most_recent_attributes_for_child_records_marked_for_destruction
  ship = Ship.create!(name: "The good ship Dollypop")
  part = ship.parts.create!(name: "Mast")
  part.mark_for_destruction
  ShipPart.find(part.id).update_columns(name: "Deck")
  assert_equal "Deck", ship.parts[0].name
end
test_should_construct_new_finder_sql_after_create() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 72
def test_should_construct_new_finder_sql_after_create
  person = Person.new first_name: "clark"
  assert_equal [], person.readers.to_a
  person.save!
  reader = Reader.create! person: person, post: Post.new(title: "foo", body: "bar")
  assert person.readers.find(reader.id)
end
test_subselect() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 39
def test_subselect
  author = authors :david
  favs = author.author_favorites
  fav2 = author.author_favorites.where(author: Author.where(id: author.id)).to_a
  assert_equal favs, fav2
end
test_using_limitable_reflections_helper() click to toggle source
# File activerecord/test/cases/associations_test.rb, line 99
def test_using_limitable_reflections_helper
  using_limitable_reflections = lambda { |reflections| Tagging.all.send :using_limitable_reflections?, reflections }
  belongs_to_reflections = [Tagging.reflect_on_association(:tag), Tagging.reflect_on_association(:super_tag)]
  has_many_reflections = [Tag.reflect_on_association(:taggings), Developer.reflect_on_association(:projects)]
  mixed_reflections = (belongs_to_reflections + has_many_reflections).uniq
  assert using_limitable_reflections.call(belongs_to_reflections), "Belong to associations are limitable"
  assert !using_limitable_reflections.call(has_many_reflections), "All has many style associations are not limitable"
  assert !using_limitable_reflections.call(mixed_reflections), "No collection associations (has many style) should pass"
end