class HasManyAssociationsTest

Public Class Methods

name() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 582
def self.name
  "Author"
end

Public Instance Methods

overwrite_to_raise() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1605
def overwrite_to_raise() raise "Trigger rollback" end
setup() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 120
def setup
  Client.destroyed_client_ids.clear
end
test_abstract_class_with_polymorphic_has_many() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2183
def test_abstract_class_with_polymorphic_has_many
  post = SubStiPost.create! title: "fooo", body: "baa"
  tagging = Tagging.create! taggable: post
  assert_equal [tagging], post.taggings
end
test_add_record_to_collection_should_change_its_updated_at() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 181
def test_add_record_to_collection_should_change_its_updated_at
  ship = Ship.create(name: "dauntless")
  part = ShipPart.create(name: "cockpit")
  updated_at = part.updated_at

  travel(1.second) do
    ship.parts << part
  end

  assert_equal part.ship, ship
  assert_not_equal part.updated_at, updated_at
end
test_adding() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 860
def test_adding
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  natural = Client.new("name" => "Natural Company")
  companies(:first_firm).clients_of_firm << natural
  assert_equal 3, companies(:first_firm).clients_of_firm.size # checking via the collection
  assert_equal 3, companies(:first_firm).clients_of_firm.reload.size # checking using the db
  assert_equal natural, companies(:first_firm).clients_of_firm.last
end
test_adding_a_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 919
def test_adding_a_collection
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
  assert_equal 4, companies(:first_firm).clients_of_firm.size
  assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
end
test_adding_a_mismatch_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 913
def test_adding_a_mismatch_class
  assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
  assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
  assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
end
test_adding_array_and_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1687
def test_adding_array_and_collection
  assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
end
test_adding_using_create() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 872
def test_adding_using_create
  first_firm = companies(:first_firm)
  assert_equal 3, first_firm.plain_clients.size
  first_firm.plain_clients.create(name: "Natural Company")
  assert_equal 4, first_firm.plain_clients.length
  assert_equal 4, first_firm.plain_clients.size
end
test_anonymous_has_many() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 130
def test_anonymous_has_many
  developer = Class.new(ActiveRecord::Base) {
    self.table_name = "developers"
    dev = self

    developer_project = Class.new(ActiveRecord::Base) {
      self.table_name = "developers_projects"
      belongs_to :developer, anonymous_class: dev
    }
    has_many :developer_projects, anonymous_class: developer_project, foreign_key: "developer_id"
  }
  dev = developer.first
  named = Developer.find(dev.id)
  assert_operator dev.developer_projects.count, :>, 0
  assert_equal named.projects.map(&:id).sort,
               dev.developer_projects.map(&:project_id).sort
end
test_assign_ids_ignoring_blanks() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1823
def test_assign_ids_ignoring_blanks
  firm = Firm.create!(name: "Apple")
  firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, ""]
  firm.save!

  assert_equal 2, firm.clients.reload.size
  assert_equal true, firm.clients.include?(companies(:second_client))
end
test_association_attributes_are_available_to_after_initialize() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2221
def test_association_attributes_are_available_to_after_initialize
  car = Car.create(name: "honda")
  bulb = car.bulbs.build

  assert_equal car.id, bulb.attributes_after_initialize["car_id"]
end
test_association_keys_bypass_attribute_protection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 336
def test_association_keys_bypass_attribute_protection
  car = Car.create(name: "honda")

  bulb = car.bulbs.new
  assert_equal car.id, bulb.car_id

  bulb = car.bulbs.new car_id: car.id + 1
  assert_equal car.id, bulb.car_id

  bulb = car.bulbs.build
  assert_equal car.id, bulb.car_id

  bulb = car.bulbs.build car_id: car.id + 1
  assert_equal car.id, bulb.car_id

  bulb = car.bulbs.create
  assert_equal car.id, bulb.car_id

  bulb = car.bulbs.create car_id: car.id + 1
  assert_equal car.id, bulb.car_id
end
test_association_protect_foreign_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 358
def test_association_protect_foreign_key
  invoice = Invoice.create

  line_item = invoice.line_items.new
  assert_equal invoice.id, line_item.invoice_id

  line_item = invoice.line_items.new invoice_id: invoice.id + 1
  assert_equal invoice.id, line_item.invoice_id

  line_item = invoice.line_items.build
  assert_equal invoice.id, line_item.invoice_id

  line_item = invoice.line_items.build invoice_id: invoice.id + 1
  assert_equal invoice.id, line_item.invoice_id

  line_item = invoice.line_items.create
  assert_equal invoice.id, line_item.invoice_id

  line_item = invoice.line_items.create invoice_id: invoice.id + 1
  assert_equal invoice.id, line_item.invoice_id
end
test_association_proxy_transaction_method_starts_transaction_in_association_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2091
def test_association_proxy_transaction_method_starts_transaction_in_association_class
  Comment.expects(:transaction)
  Post.first.comments.transaction do
    # nothing
  end
end
test_attributes_are_being_set_when_initialized_from_has_many_association_with_multiple_where_clauses() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2132
def test_attributes_are_being_set_when_initialized_from_has_many_association_with_multiple_where_clauses
  new_comment = posts(:welcome).comments.where(body: "Some content").where(type: "SpecialComment").build
  assert_equal new_comment.body, "Some content"
  assert_equal new_comment.type, "SpecialComment"
  assert_equal new_comment.post_id, posts(:welcome).id
end
test_attributes_are_being_set_when_initialized_from_has_many_association_with_where_clause() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2127
def test_attributes_are_being_set_when_initialized_from_has_many_association_with_where_clause
  new_comment = posts(:welcome).comments.where(body: "Some content").build
  assert_equal new_comment.body, "Some content"
end
test_attributes_are_set_when_initialized_from_has_many_null_relationship() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2228
def test_attributes_are_set_when_initialized_from_has_many_null_relationship
  car  = Car.new name: "honda"
  bulb = car.bulbs.where(name: "headlight").first_or_initialize
  assert_equal "headlight", bulb.name
end
test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2234
def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship
  post    = Post.new title: "title", body: "bar"
  tag     = Tag.create!(name: "foo")

  tagging = post.taggings.where(tag: tag).first_or_initialize

  assert_equal tag.id, tagging.tag_id
  assert_equal "Post", tagging.taggable_type
end
test_belongs_to_sanity() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 656
def test_belongs_to_sanity
  c = Client.new
  assert_nil c.firm, "belongs_to failed sanity check on new object"
end
test_build() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 965
def test_build
  company = companies(:first_firm)
  new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build("name" => "Another Client") }
  assert !company.clients_of_firm.loaded?

  assert_equal "Another Client", new_client.name
  assert !new_client.persisted?
  assert_equal new_client, company.clients_of_firm.last
end
test_build_and_create_from_association_should_respect_passed_attributes_over_default_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 221
def test_build_and_create_from_association_should_respect_passed_attributes_over_default_scope
  car = Car.create(name: "honda")

  bulb = car.bulbs.build(name: "exotic")
  assert_equal "exotic", bulb.name

  bulb = car.bulbs.create(name: "exotic")
  assert_equal "exotic", bulb.name

  bulb = car.awesome_bulbs.build(frickinawesome: false)
  assert_equal false, bulb.frickinawesome

  bulb = car.awesome_bulbs.create(frickinawesome: false)
  assert_equal false, bulb.frickinawesome
end
test_build_and_create_should_not_happen_within_scope() click to toggle source

When creating objects on the association, we must not do it within a scope (even though it would be convenient), because this would cause that scope to be applied to any callbacks etc.

# File activerecord/test/cases/associations/has_many_associations_test.rb, line 382
def test_build_and_create_should_not_happen_within_scope
  car = cars(:honda)
  scope = car.foo_bulbs.where_values_hash

  bulb = car.foo_bulbs.build
  assert_not_equal scope, bulb.scope_after_initialize.where_values_hash

  bulb = car.foo_bulbs.create
  assert_not_equal scope, bulb.scope_after_initialize.where_values_hash

  bulb = car.foo_bulbs.create!
  assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
end
test_build_followed_by_save_does_not_load_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1008
def test_build_followed_by_save_does_not_load_target
  companies(:first_firm).clients_of_firm.build("name" => "Another Client")
  assert companies(:first_firm).save
  assert !companies(:first_firm).clients_of_firm.loaded?
end
test_build_from_association_sets_inverse_instance() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 257
def test_build_from_association_sets_inverse_instance
  car = Car.new(name: "honda")

  bulb = car.bulbs.build
  assert_equal car, bulb.car
end
test_build_from_association_should_respect_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 237
def test_build_from_association_should_respect_scope
  author = Author.new

  post = author.thinking_posts.build
  assert_equal "So I was thinking", post.title
end
test_build_from_polymorphic_association_sets_inverse_instance() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2206
def test_build_from_polymorphic_association_sets_inverse_instance
  post = Post.new
  tagging = post.taggings.build

  assert_equal post, tagging.taggable
end
test_build_many() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1002
def test_build_many
  company = companies(:first_firm)
  new_clients = assert_no_queries(ignore_none: false) { company.clients_of_firm.build([{ "name" => "Another Client" }, { "name" => "Another Client II" }]) }
  assert_equal 2, new_clients.size
end
test_build_many_via_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1038
def test_build_many_via_block
  company = companies(:first_firm)
  new_clients = assert_no_queries(ignore_none: false) do
    company.clients_of_firm.build([{ "name" => "Another Client" }, { "name" => "Another Client II" }]) do |client|
      client.name = "changed"
    end
  end

  assert_equal 2, new_clients.size
  assert_equal "changed", new_clients.first.name
  assert_equal "changed", new_clients.last.name
end
test_build_via_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1028
def test_build_via_block
  company = companies(:first_firm)
  new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build { |client| client.name = "Another Client" } }
  assert !company.clients_of_firm.loaded?

  assert_equal "Another Client", new_client.name
  assert !new_client.persisted?
  assert_equal new_client, company.clients_of_firm.last
end
test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2198
def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id
  welcome = posts(:welcome)
  tagging = welcome.taggings.build(taggable_id: 99, taggable_type: "ShouldNotChange")

  assert_equal welcome.id, tagging.taggable_id
  assert_equal "Post", tagging.taggable_type
end
test_build_without_loading_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1014
def test_build_without_loading_association
  first_topic = topics(:first)
  Reply.column_names

  assert_equal 1, first_topic.replies.length

  assert_no_queries do
    first_topic.replies.build(title: "Not saved", content: "Superstars")
    assert_equal 2, first_topic.replies.size
  end

  assert_equal 2, first_topic.replies.to_ary.size
end
test_building_the_associated_object_with_an_invalid_type() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 316
def test_building_the_associated_object_with_an_invalid_type
  firm = DependentFirm.new
  assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(type: "Invalid") }
end
test_building_the_associated_object_with_an_unrelated_type() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 321
def test_building_the_associated_object_with_an_unrelated_type
  firm = DependentFirm.new
  assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(type: "Account") }
end
test_building_the_associated_object_with_explicit_sti_base_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 304
def test_building_the_associated_object_with_explicit_sti_base_class
  firm = DependentFirm.new
  company = firm.companies.build(type: "Company")
  assert_kind_of Company, company, "Expected #{company.class} to be a Company"
end
test_building_the_associated_object_with_implicit_sti_base_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 298
def test_building_the_associated_object_with_implicit_sti_base_class
  firm = DependentFirm.new
  company = firm.companies.build
  assert_kind_of Company, company, "Expected #{company.class} to be a Company"
end
test_building_the_associated_object_with_sti_subclass() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 310
def test_building_the_associated_object_with_sti_subclass
  firm = DependentFirm.new
  company = firm.companies.build(type: "Client")
  assert_kind_of Client, company, "Expected #{company.class} to be a Client"
end
test_calling_empty_with_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1199
def test_calling_empty_with_counter_cache
  post = posts(:welcome)
  assert_queries(0) do
    assert_not post.comments.empty?
  end
end
test_calling_first_nth_or_last_on_association_should_not_load_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1887
def test_calling_first_nth_or_last_on_association_should_not_load_association
  firm = companies(:first_firm)
  firm.clients.first
  firm.clients.second
  firm.clients.last
  assert !firm.clients.loaded?
end
test_calling_first_nth_or_last_on_existing_record_with_create_should_not_load_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1922
def test_calling_first_nth_or_last_on_existing_record_with_create_should_not_load_association
  firm = companies(:first_firm)
  firm.clients.create(name: "Foo")
  assert !firm.clients.loaded?

  assert_queries 3 do
    firm.clients.first
    firm.clients.second
    firm.clients.last
  end

  assert !firm.clients.loaded?
end
test_calling_first_nth_or_last_on_new_record_should_not_run_queries() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1936
def test_calling_first_nth_or_last_on_new_record_should_not_run_queries
  firm = Firm.new

  assert_no_queries do
    firm.clients.first
    firm.clients.second
    firm.clients.last
  end
end
test_calling_first_or_last_on_existing_record_with_build_should_load_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1908
def test_calling_first_or_last_on_existing_record_with_build_should_load_association
  firm = companies(:first_firm)
  firm.clients.build(name: "Foo")
  assert !firm.clients.loaded?

  assert_queries 1 do
    firm.clients.first
    firm.clients.second
    firm.clients.last
  end

  assert firm.clients.loaded?
end
test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1895
def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
  firm = companies(:first_firm)
  firm.clients.load_target
  assert firm.clients.loaded?

  assert_no_queries(ignore_none: false) do
    firm.clients.first
    assert_equal 2, firm.clients.first(2).size
    firm.clients.last
    assert_equal 2, firm.clients.last(2).size
  end
end
test_calling_first_or_last_with_integer_on_association_should_not_load_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1946
def test_calling_first_or_last_with_integer_on_association_should_not_load_association
  firm = companies(:first_firm)
  firm.clients.create(name: "Foo")
  assert !firm.clients.loaded?

  assert_queries 2 do
    firm.clients.first(2)
    firm.clients.last(2)
  end

  assert !firm.clients.loaded?
end
test_calling_many_on_loaded_association_should_not_use_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1967
def test_calling_many_on_loaded_association_should_not_use_query
  firm = companies(:first_firm)
  firm.clients.load  # force load
  assert_no_queries { assert firm.clients.many? }
end
test_calling_many_should_count_instead_of_loading_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1959
def test_calling_many_should_count_instead_of_loading_association
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.many?  # use count query
  end
  assert !firm.clients.loaded?
end
test_calling_many_should_defer_to_collection_if_using_a_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1973
def test_calling_many_should_defer_to_collection_if_using_a_block
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.expects(:size).never
    firm.clients.many? { true }
  end
  assert firm.clients.loaded?
end
test_calling_many_should_return_false_if_none_or_one() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1982
def test_calling_many_should_return_false_if_none_or_one
  firm = companies(:another_firm)
  assert !firm.clients_like_ms.many?
  assert_equal 0, firm.clients_like_ms.size

  firm = companies(:first_firm)
  assert !firm.limited_clients.many?
  assert_equal 1, firm.limited_clients.size
end
test_calling_many_should_return_true_if_more_than_one() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1992
def test_calling_many_should_return_true_if_more_than_one
  firm = companies(:first_firm)
  assert firm.clients.many?
  assert_equal 3, firm.clients.size
end
test_calling_none_on_loaded_association_should_not_use_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2006
def test_calling_none_on_loaded_association_should_not_use_query
  firm = companies(:first_firm)
  firm.clients.load  # force load
  assert_no_queries { assert ! firm.clients.none? }
end
test_calling_none_should_count_instead_of_loading_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1998
def test_calling_none_should_count_instead_of_loading_association
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.none?  # use count query
  end
  assert !firm.clients.loaded?
end
test_calling_none_should_defer_to_collection_if_using_a_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2012
def test_calling_none_should_defer_to_collection_if_using_a_block
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.expects(:size).never
    firm.clients.none? { true }
  end
  assert firm.clients.loaded?
end
test_calling_none_should_return_false_if_any() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2027
def test_calling_none_should_return_false_if_any
  firm = companies(:first_firm)
  assert !firm.limited_clients.none?
  assert_equal 1, firm.limited_clients.size
end
test_calling_none_should_return_true_if_none() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2021
def test_calling_none_should_return_true_if_none
  firm = companies(:another_firm)
  assert firm.clients_like_ms.none?
  assert_equal 0, firm.clients_like_ms.size
end
test_calling_one_on_loaded_association_should_not_use_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2041
def test_calling_one_on_loaded_association_should_not_use_query
  firm = companies(:first_firm)
  firm.clients.load  # force load
  assert_no_queries { assert ! firm.clients.one? }
end
test_calling_one_should_count_instead_of_loading_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2033
def test_calling_one_should_count_instead_of_loading_association
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.one?  # use count query
  end
  assert !firm.clients.loaded?
end
test_calling_one_should_defer_to_collection_if_using_a_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2047
def test_calling_one_should_defer_to_collection_if_using_a_block
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients.expects(:size).never
    firm.clients.one? { true }
  end
  assert firm.clients.loaded?
end
test_calling_one_should_return_false_if_more_than_one() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2068
def test_calling_one_should_return_false_if_more_than_one
  firm = companies(:first_firm)
  assert ! firm.clients.one?
  assert_equal 3, firm.clients.size
end
test_calling_one_should_return_false_if_zero() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2056
def test_calling_one_should_return_false_if_zero
  firm = companies(:another_firm)
  assert ! firm.clients_like_ms.one?
  assert_equal 0, firm.clients_like_ms.size
end
test_calling_one_should_return_true_if_one() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2062
def test_calling_one_should_return_true_if_one
  firm = companies(:first_firm)
  assert firm.limited_clients.one?
  assert_equal 1, firm.limited_clients.size
end
test_calling_update_attributes_changing_ids_doesnt_change_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1227
def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
  topic1 = Topic.find(1)
  topic2 = Topic.find(3)
  original_count1 = topic1.replies.to_a.size
  original_count2 = topic2.replies.to_a.size

  reply1 = topic1.replies.first
  reply2 = topic2.replies.first

  reply1.update_attributes(parent_id: topic2.id)
  assert_equal original_count1 - 1, topic1.reload.replies_count
  assert_equal original_count2 + 1, topic2.reload.replies_count

  reply2.update_attributes(parent_id: topic1.id)
  assert_equal original_count1, topic1.reload.replies_count
  assert_equal original_count2, topic2.reload.replies_count
end
test_calling_update_attributes_on_id_changes_the_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1214
def test_calling_update_attributes_on_id_changes_the_counter_cache
  topic = Topic.order("id ASC").first
  original_count = topic.replies.to_a.size
  assert_equal original_count, topic.replies_count

  first_reply = topic.replies.first
  first_reply.update_attributes(parent_id: nil)
  assert_equal original_count - 1, topic.reload.replies_count

  first_reply.update_attributes(parent_id: topic.id)
  assert_equal original_count, topic.reload.replies_count
end
test_cant_save_has_many_readonly_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 613
def test_cant_save_has_many_readonly_association
  authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
  authors(:david).readonly_comments.each { |c| assert c.readonly? }
end
test_clear_collection_should_not_change_updated_at() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 194
def test_clear_collection_should_not_change_updated_at
  # GH#17161: .clear calls delete_all (and returns the association),
  # which is intended to not touch associated objects's updated_at field
  ship = Ship.create(name: "dauntless")
  part = ShipPart.create(name: "cockpit", ship_id: ship.id)

  ship.parts.clear
  part.reload

  assert_nil part.ship
  assert !part.updated_at_changed?
end
test_clearing_a_dependent_association_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1341
def test_clearing_a_dependent_association_collection
  firm = companies(:first_firm)
  client_id = firm.dependent_clients_of_firm.first.id
  assert_equal 2, firm.dependent_clients_of_firm.size
  assert_equal 1, Client.find_by_id(client_id).client_of

  # :delete_all is called on each client since the dependent options is :destroy
  firm.dependent_clients_of_firm.clear

  assert_equal 0, firm.dependent_clients_of_firm.size
  assert_equal 0, firm.dependent_clients_of_firm.reload.size
  assert_equal [], Client.destroyed_client_ids[firm.id]

  # Should be destroyed since the association is dependent.
  assert_nil Client.find_by_id(client_id)
end
test_clearing_an_association_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1307
def test_clearing_an_association_collection
  firm = companies(:first_firm)
  client_id = firm.clients_of_firm.first.id
  assert_equal 2, firm.clients_of_firm.size

  firm.clients_of_firm.clear

  assert_equal 0, firm.clients_of_firm.size
  assert_equal 0, firm.clients_of_firm.reload.size
  assert_equal [], Client.destroyed_client_ids[firm.id]

  # Should not be destroyed since the association is not dependent.
  assert_nothing_raised do
    assert_nil Client.find(client_id).firm
  end
end
test_clearing_an_exclusively_dependent_association_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1372
def test_clearing_an_exclusively_dependent_association_collection
  firm = companies(:first_firm)
  client_id = firm.exclusively_dependent_clients_of_firm.first.id
  assert_equal 2, firm.exclusively_dependent_clients_of_firm.size

  assert_equal [], Client.destroyed_client_ids[firm.id]

  # :exclusively_dependent means each client is deleted directly from
  # the database without looping through them calling destroy.
  firm.exclusively_dependent_clients_of_firm.clear

  assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
  assert_equal 0, firm.exclusively_dependent_clients_of_firm.reload.size
  # no destroy-filters should have been called
  assert_equal [], Client.destroyed_client_ids[firm.id]

  # Should be destroyed since the association is exclusively dependent.
  assert_nil Client.find_by_id(client_id)
end
test_clearing_updates_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1324
def test_clearing_updates_counter_cache
  topic = Topic.first

  assert_difference "topic.reload.replies_count", -1 do
    topic.replies.clear
  end
end
test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1332
def test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy
  car = Car.first
  car.engines.create!

  assert_difference "car.reload.engines_count", -1 do
    car.engines.clear
  end
end
test_clearing_without_initial_access() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1453
def test_clearing_without_initial_access
  firm = companies(:first_firm)

  firm.clients_of_firm.clear

  assert_equal 0, firm.clients_of_firm.size
  assert_equal 0, firm.clients_of_firm.reload.size
end
test_collection_association_with_private_kernel_method() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2267
def test_collection_association_with_private_kernel_method
  firm = companies(:first_firm)
  assert_equal [accounts(:signals37)], firm.accounts.open
end
test_collection_not_empty_after_building() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 983
def test_collection_not_empty_after_building
  company = companies(:first_firm)
  assert_predicate company.contracts, :empty?
  company.contracts.build
  assert_not_predicate company.contracts, :empty?
end
test_collection_size_after_building() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 975
def test_collection_size_after_building
  company = companies(:first_firm)  # company already has one client
  company.clients_of_firm.build("name" => "Another Client")
  company.clients_of_firm.build("name" => "Yet Another Client")
  assert_equal 4, company.clients_of_firm.size
  assert_equal 4, company.clients_of_firm.uniq.size
end
test_collection_size_twice_for_regressions() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 990
def test_collection_size_twice_for_regressions
  post = posts(:thinking)
  assert_equal 0, post.readers.size
  # This test needs a post that has no readers, we assert it to ensure it holds,
  # but need to reload the post because the very call to #size hides the bug.
  post.reload
  post.readers.build
  size1 = post.readers.size
  size2 = post.readers.size
  assert_equal size1, size2
end
test_counter_cache_on_unloaded_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1783
def test_counter_cache_on_unloaded_association
  car = Car.create(name: "My AppliCar")
  assert_equal car.engines.size, 0
end
test_counter_cache_updates_in_memory_after_concat() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1134
def test_counter_cache_updates_in_memory_after_concat
  topic = Topic.create title: "Zoom-zoom-zoom"

  topic.replies << Reply.create(title: "re: zoom", content: "speedy quick!")
  assert_equal 1, topic.replies_count
  assert_equal 1, topic.replies.size
  assert_equal 1, topic.reload.replies.size
end
test_counter_cache_updates_in_memory_after_create() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1143
def test_counter_cache_updates_in_memory_after_create
  topic = Topic.create title: "Zoom-zoom-zoom"

  topic.replies.create!(title: "re: zoom", content: "speedy quick!")
  assert_equal 1, topic.replies_count
  assert_equal 1, topic.replies.size
  assert_equal 1, topic.reload.replies.size
end
test_counter_cache_updates_in_memory_after_create_with_array() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1152
def test_counter_cache_updates_in_memory_after_create_with_array
  topic = Topic.create title: "Zoom-zoom-zoom"

  topic.replies.create!([
    { title: "re: zoom", content: "speedy quick!" },
    { title: "re: zoom 2", content: "OMG lol!" },
  ])
  assert_equal 2, topic.replies_count
  assert_equal 2, topic.replies.size
  assert_equal 2, topic.reload.replies.size
end
test_counting() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 520
def test_counting
  assert_equal 3, Firm.first.plain_clients.count
end
test_counting_with_association_limit() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 532
def test_counting_with_association_limit
  firm = companies(:first_firm)
  assert_equal firm.limited_clients.length, firm.limited_clients.size
  assert_equal firm.limited_clients.length, firm.limited_clients.count
end
test_counting_with_column_name_and_hash() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 528
def test_counting_with_column_name_and_hash
  assert_equal 3, Firm.first.plain_clients.count(:name)
end
test_counting_with_counter_sql() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 516
def test_counting_with_counter_sql
  assert_equal 3, Firm.first.clients.count
end
test_counting_with_single_hash() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 524
def test_counting_with_single_hash
  assert_equal 1, Firm.first.plain_clients.where(name: "Microsoft").count
end
test_create() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1066
def test_create
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  assert new_client.persisted?
  assert_equal new_client, companies(:first_firm).clients_of_firm.last
  assert_equal new_client, companies(:first_firm).clients_of_firm.reload.last
end
test_create_followed_by_save_does_not_load_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1082
def test_create_followed_by_save_does_not_load_target
  companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  assert companies(:first_firm).save
  assert !companies(:first_firm).clients_of_firm.loaded?
end
test_create_from_association_should_respect_default_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 207
def test_create_from_association_should_respect_default_scope
  car = Car.create(name: "honda")
  assert_equal "honda", car.name

  bulb = Bulb.create
  assert_equal "defaulty", bulb.name

  bulb = car.bulbs.build
  assert_equal "defaulty", bulb.name

  bulb = car.bulbs.create
  assert_equal "defaulty", bulb.name
end
test_create_from_association_with_nil_values_should_work() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 244
def test_create_from_association_with_nil_values_should_work
  car = Car.create(name: "honda")

  bulb = car.bulbs.new(nil)
  assert_equal "defaulty", bulb.name

  bulb = car.bulbs.build(nil)
  assert_equal "defaulty", bulb.name

  bulb = car.bulbs.create(nil)
  assert_equal "defaulty", bulb.name
end
test_create_many() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1077
def test_create_many
  companies(:first_firm).clients_of_firm.create([{ "name" => "Another Client" }, { "name" => "Another Client II" }])
  assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
end
test_create_resets_cached_counters() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 478
def test_create_resets_cached_counters
  Reader.delete_all

  person = Person.create!(first_name: "tenderlove")

  post   = Post.first

  assert_equal [], person.readers
  assert_nil person.readers.find_by_post_id(post.id)

  person.readers.create(post_id: post.id)

  assert_equal 1, person.readers.count
  assert_equal 1, person.readers.length
  assert_equal post, person.readers.first.post
  assert_equal person, person.readers.first.person
end
test_create_with_bang_on_habtm_when_parent_is_new_raises() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 905
def test_create_with_bang_on_habtm_when_parent_is_new_raises
  error = assert_raise(ActiveRecord::RecordNotSaved) do
    Developer.new("name" => "Aredridel").projects.create!
  end

  assert_equal "You cannot call create unless the parent is saved", error.message
end
test_create_with_bang_on_has_many_raises_when_record_not_saved() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 898
def test_create_with_bang_on_has_many_raises_when_record_not_saved
  assert_raise(ActiveRecord::RecordInvalid) do
    firm = Firm.first
    firm.plain_clients.create!
  end
end
test_create_with_bang_on_has_many_when_parent_is_new_raises() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 880
def test_create_with_bang_on_has_many_when_parent_is_new_raises
  error = assert_raise(ActiveRecord::RecordNotSaved) do
    firm = Firm.new
    firm.plain_clients.create! name: "Whoever"
  end

  assert_equal "You cannot call create unless the parent is saved", error.message
end
test_create_without_loading_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1051
def test_create_without_loading_association
  first_firm = companies(:first_firm)
  Firm.column_names
  Client.column_names

  assert_equal 2, first_firm.clients_of_firm.size
  first_firm.clients_of_firm.reset

  assert_queries(1) do
    first_firm.clients_of_firm.create(name: "Superstars")
  end

  assert_equal 3, first_firm.clients_of_firm.size
end
test_creating_using_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2103
def test_creating_using_primary_key
  firm = Firm.first
  client = firm.clients_using_primary_key.create!(name: "test")
  assert_equal firm.name, client.firm_name
end
test_creation_respects_hash_condition() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1441
def test_creation_respects_hash_condition
  ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build

  assert        ms_client.save
  assert_equal  "Microsoft", ms_client.name

  another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create

  assert        another_ms_client.persisted?
  assert_equal  "Microsoft", another_ms_client.name
end
test_custom_named_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1206
def test_custom_named_counter_cache
  topic = topics(:first)

  assert_difference "topic.reload.replies_count", -1 do
    topic.approved_replies.clear
  end
end
test_default_scope_on_relations_is_not_cached() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 148
def test_default_scope_on_relations_is_not_cached
  counter = 0
  posts = Class.new(ActiveRecord::Base) {
    self.table_name = "posts"
    self.inheritance_column = "not_there"
    post = self

    comments = Class.new(ActiveRecord::Base) {
      self.table_name = "comments"
      self.inheritance_column = "not_there"
      belongs_to :post, anonymous_class: post
      default_scope -> {
        counter += 1
        where("id = :inc", inc: counter)
      }
    }
    has_many :comments, anonymous_class: comments, foreign_key: "post_id"
  }
  assert_equal 0, counter
  post = posts.first
  assert_equal 0, counter
  sql = capture_sql { post.comments.to_a }
  post.comments.reset
  assert_not_equal sql, capture_sql { post.comments.to_a }
end
test_default_select() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 838
def test_default_select
  assert_equal Comment.column_names.sort, posts(:welcome).comments.first.attributes.keys.sort
end
test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2109
  def test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class
    ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
    class_eval(<<-EOF, __FILE__, __LINE__ + 1)
      class DeleteAllModel < ActiveRecord::Base
        has_many :nonentities, :dependent => :delete_all
      end
    EOF
  end
test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2118
  def test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class
    ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
    class_eval(<<-EOF, __FILE__, __LINE__ + 1)
      class NullifyModel < ActiveRecord::Base
        has_many :nonentities, :dependent => :nullify
      end
    EOF
  end
test_delete_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1257
def test_delete_all
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).dependent_clients_of_firm.create("name" => "Another Client")
  clients = companies(:first_firm).dependent_clients_of_firm.to_a
  assert_equal 3, clients.count

  assert_difference "Client.count", -(clients.count) do
    companies(:first_firm).dependent_clients_of_firm.delete_all
  end
end
test_delete_all_accepts_limited_parameters() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1365
def test_delete_all_accepts_limited_parameters
  firm = companies(:first_firm)
  assert_raise(ArgumentError) do
    firm.dependent_clients_of_firm.delete_all(:destroy)
  end
end
test_delete_all_association_with_primary_key_deletes_correct_records() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1428
def test_delete_all_association_with_primary_key_deletes_correct_records
  firm = Firm.first
  # break the vanilla firm_id foreign key
  assert_equal 3, firm.clients.count
  firm.clients.first.update_columns(firm_id: nil)
  assert_equal 2, firm.clients.reload.count
  assert_equal 2, firm.clients_using_primary_key_with_delete_all.count
  old_record = firm.clients_using_primary_key_with_delete_all.first
  firm = Firm.first
  firm.destroy
  assert_nil Client.find_by_id(old_record.id)
end
test_delete_all_on_association_is_the_same_as_not_loaded() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 272
def test_delete_all_on_association_is_the_same_as_not_loaded
  author = authors :david
  author.thinking_posts.create!(body: "test")
  author.reload
  expected_sql = capture_sql { author.thinking_posts.delete_all }

  author.thinking_posts.create!(body: "test")
  author.reload
  author.thinking_posts.inspect
  loaded_sql = capture_sql { author.thinking_posts.delete_all }
  assert_equal(expected_sql, loaded_sql)
end
test_delete_all_on_association_with_nil_dependency_is_the_same_as_not_loaded() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 285
def test_delete_all_on_association_with_nil_dependency_is_the_same_as_not_loaded
  author = authors :david
  author.posts.create!(title: "test", body: "body")
  author.reload
  expected_sql = capture_sql { author.posts.delete_all }

  author.posts.create!(title: "test", body: "body")
  author.reload
  author.posts.to_a
  loaded_sql = capture_sql { author.posts.delete_all }
  assert_equal(expected_sql, loaded_sql)
end
test_delete_all_with_not_yet_loaded_association_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1271
def test_delete_all_with_not_yet_loaded_association_collection
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  assert_equal 3, companies(:first_firm).clients_of_firm.size
  companies(:first_firm).clients_of_firm.reset
  companies(:first_firm).clients_of_firm.delete_all
  assert_equal 0, companies(:first_firm).clients_of_firm.size
  assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
end
test_delete_all_with_option_delete_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1358
def test_delete_all_with_option_delete_all
  firm = companies(:first_firm)
  client_id = firm.dependent_clients_of_firm.first.id
  firm.dependent_clients_of_firm.delete_all(:delete_all)
  assert_nil Client.find_by_id(client_id)
end
test_deleting() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1088
def test_deleting
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
  assert_equal 1, companies(:first_firm).clients_of_firm.size
  assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
end
test_deleting_a_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1245
def test_deleting_a_collection
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  assert_equal 3, companies(:first_firm).clients_of_firm.size
  companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1], companies(:first_firm).clients_of_firm[2]])
  assert_equal 0, companies(:first_firm).clients_of_firm.size
  assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
end
test_deleting_a_item_which_is_not_in_the_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1462
def test_deleting_a_item_which_is_not_in_the_collection
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  summit = Client.find_by_name("Summit")
  companies(:first_firm).clients_of_firm.delete(summit)
  assert_equal 2, companies(:first_firm).clients_of_firm.size
  assert_equal 2, companies(:first_firm).clients_of_firm.reload.size
  assert_equal 2, summit.client_of
end
test_deleting_before_save() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1098
def test_deleting_before_save
  new_firm = Firm.new("name" => "A New Firm, Inc.")
  new_client = new_firm.clients_of_firm.build("name" => "Another Client")
  assert_equal 1, new_firm.clients_of_firm.size
  new_firm.clients_of_firm.delete(new_client)
  assert_equal 0, new_firm.clients_of_firm.size
end
test_deleting_by_integer_id() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1474
def test_deleting_by_integer_id
  david = Developer.find(1)

  assert_difference "david.projects.count", -1 do
    assert_equal 1, david.projects.delete(1).size
  end

  assert_equal 1, david.projects.size
end
test_deleting_by_string_id() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1484
def test_deleting_by_string_id
  david = Developer.find(1)

  assert_difference "david.projects.count", -1 do
    assert_equal 1, david.projects.delete("1").size
  end

  assert_equal 1, david.projects.size
end
test_deleting_self_type_mismatch() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1494
def test_deleting_self_type_mismatch
  david = Developer.find(1)
  david.projects.reload
  assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
end
test_deleting_updates_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1125
def test_deleting_updates_counter_cache
  topic = Topic.order("id ASC").first
  assert_equal topic.replies.to_a.size, topic.replies_count

  topic.replies.delete(topic.replies.first)
  topic.reload
  assert_equal topic.replies.to_a.size, topic.replies_count
end
test_deleting_updates_counter_cache_with_dependent_delete_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1181
def test_deleting_updates_counter_cache_with_dependent_delete_all
  post = posts(:welcome)
  post.update_columns(taggings_with_delete_all_count: post.tags_count)

  assert_difference "post.reload.taggings_with_delete_all_count", -1 do
    post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first)
  end
end
test_deleting_updates_counter_cache_with_dependent_destroy() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1190
def test_deleting_updates_counter_cache_with_dependent_destroy
  post = posts(:welcome)
  post.update_columns(taggings_with_destroy_count: post.tags_count)

  assert_difference "post.reload.taggings_with_destroy_count", -1 do
    post.taggings_with_destroy.delete(post.taggings_with_destroy.first)
  end
end
test_deleting_updates_counter_cache_without_dependent_option() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1173
def test_deleting_updates_counter_cache_without_dependent_option
  post = posts(:welcome)

  assert_difference "post.reload.tags_count", -1 do
    post.taggings.delete(post.taggings.first)
  end
end
test_dependence() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1569
def test_dependence
  firm = companies(:first_firm)
  assert_equal 3, firm.clients.size
  firm.destroy
  assert Client.all.merge!(where: "firm_id=#{firm.id}").to_a.empty?
end
test_dependence_for_associations_with_hash_condition() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1576
def test_dependence_for_associations_with_hash_condition
  david = authors(:david)
  assert_difference("Post.count", -1) { assert david.destroy }
end
test_dependence_on_account() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1612
def test_dependence_on_account
  num_accounts = Account.count
  companies(:first_firm).destroy
  assert_equal num_accounts - 1, Account.count
end
test_dependence_with_transaction_support_on_failure() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1601
def test_dependence_with_transaction_support_on_failure
  firm = companies(:first_firm)
  clients = firm.clients
  assert_equal 3, clients.length
  clients.last.instance_eval { def overwrite_to_raise() raise "Trigger rollback" end }

  firm.destroy rescue "do nothing"

  assert_equal 3, Client.all.merge!(where: "firm_id=#{firm.id}").to_a.size
end
test_dependent_association_respects_optional_conditions_on_delete() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1392
def test_dependent_association_respects_optional_conditions_on_delete
  firm = companies(:odegy)
  Client.create(client_of: firm.id, name: "BigShot Inc.")
  Client.create(client_of: firm.id, name: "SmallTime Inc.")
  # only one of two clients is included in the association due to the :conditions key
  assert_equal 2, Client.where(client_of: firm.id).size
  assert_equal 1, firm.dependent_conditional_clients_of_firm.size
  firm.destroy
  # only the correctly associated client should have been deleted
  assert_equal 1, Client.where(client_of: firm.id).size
end
test_dependent_association_respects_optional_hash_conditions_on_delete() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1416
def test_dependent_association_respects_optional_hash_conditions_on_delete
  firm = companies(:odegy)
  Client.create(client_of: firm.id, name: "BigShot Inc.")
  Client.create(client_of: firm.id, name: "SmallTime Inc.")
  # only one of two clients is included in the association due to the :conditions key
  assert_equal 2, Client.where(client_of: firm.id).size
  assert_equal 1, firm.dependent_hash_conditional_clients_of_firm.size
  firm.destroy
  # only the correctly associated client should have been deleted
  assert_equal 1, Client.where(client_of: firm.id).size
end
test_dependent_association_respects_optional_sanitized_conditions_on_delete() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1404
def test_dependent_association_respects_optional_sanitized_conditions_on_delete
  firm = companies(:odegy)
  Client.create(client_of: firm.id, name: "BigShot Inc.")
  Client.create(client_of: firm.id, name: "SmallTime Inc.")
  # only one of two clients is included in the association due to the :conditions key
  assert_equal 2, Client.where(client_of: firm.id).size
  assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
  firm.destroy
  # only the correctly associated client should have been deleted
  assert_equal 1, Client.where(client_of: firm.id).size
end
test_depends_and_nullify() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1618
def test_depends_and_nullify
  num_accounts = Account.count

  core = companies(:quails_core)
  assert_equal accounts(:quails_core_account), core.account
  assert_equal companies(:leetsoft, :jadedpixel), core.companies
  core.destroy
  assert_nil accounts(:quails_core_account).reload.firm_id
  assert_nil companies(:leetsoft).reload.client_of
  assert_nil companies(:jadedpixel).reload.client_of

  assert_equal num_accounts, Account.count
end
test_destroy_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1555
def test_destroy_all
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  clients = companies(:first_firm).clients_of_firm.to_a
  assert !clients.empty?, "37signals has clients after load"
  destroyed = companies(:first_firm).clients_of_firm.destroy_all
  assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
  assert destroyed.all?(&:frozen?), "destroyed clients should be frozen"
  assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
  assert companies(:first_firm).clients_of_firm.reload.empty?, "37signals has no clients after destroy all and refresh"
end
test_destroy_dependent_when_deleted_from_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1581
def test_destroy_dependent_when_deleted_from_association
  firm = Firm.first
  assert_equal 3, firm.clients.size

  client = firm.clients.first
  firm.clients.delete(client)

  assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
  assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
  assert_equal 2, firm.clients.size
end
test_destroy_does_not_raise_when_association_errors_on_destroy() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2560
def test_destroy_does_not_raise_when_association_errors_on_destroy
  assert_no_difference "AuthorWithErrorDestroyingAssociation.count" do
    author = AuthorWithErrorDestroyingAssociation.first

    assert_not author.destroy
  end
end
test_destroy_with_bang_bubbles_errors_from_associations() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2568
def test_destroy_with_bang_bubbles_errors_from_associations
  error = assert_raises ActiveRecord::RecordNotDestroyed do
    AuthorWithErrorDestroyingAssociation.first.destroy!
  end

  assert_instance_of PostWithErrorDestroying, error.record
end
test_destroying() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1500
def test_destroying
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  assert_difference "Client.count", -1 do
    companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
  end

  assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
  assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
end
test_destroying_a_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1539
def test_destroying_a_collection
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  assert_equal 3, companies(:first_firm).clients_of_firm.size

  assert_difference "Client.count", -2 do
    companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
  end

  assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
  assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
end
test_destroying_by_integer_id() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1513
def test_destroying_by_integer_id
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  assert_difference "Client.count", -1 do
    companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
  end

  assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
  assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
end
test_destroying_by_string_id() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1526
def test_destroying_by_string_id
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  assert_difference "Client.count", -1 do
    companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
  end

  assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
  assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
end
test_do_not_call_callbacks_for_delete_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 264
def test_do_not_call_callbacks_for_delete_all
  car = Car.create(name: "honda")
  car.funky_bulbs.create!
  assert_equal 1, car.funky_bulbs.count
  assert_nothing_raised { car.reload.funky_bulbs.delete_all }
  assert_equal 0, car.funky_bulbs.count, "bulbs should have been deleted using :delete_all strategy"
end
test_dont_call_save_callbacks_twice_on_has_many() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2213
def test_dont_call_save_callbacks_twice_on_has_many
  firm = companies(:first_firm)
  contract = firm.contracts.create!

  assert_equal 1, contract.hi_count
  assert_equal 1, contract.bye_count
end
test_dynamic_find_should_respect_association_order() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 557
def test_dynamic_find_should_respect_association_order
  assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.where("type = 'Client'").first
  assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.find_by_type("Client")
end
test_dynamic_find_should_respect_association_order_for_through() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1845
def test_dynamic_find_should_respect_association_order_for_through
  assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first
  assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type("SpecialComment")
end
test_exists_respects_association_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 506
def test_exists_respects_association_scope
  person = Person.new
  person.first_name = "Sasuke"
  person.references << Reference.new
  person.id = 10
  person.references
  person.save!
  assert_predicate person.references, :exists?
end
test_find_all() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 707
def test_find_all
  firm = Firm.first
  assert_equal 3, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length
  assert_equal 1, firm.clients.where("name = 'Summit'").to_a.length
end
test_find_all_sanitized() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 752
def test_find_all_sanitized
  firm = Firm.first
  summit = firm.clients.where("name = 'Summit'").to_a
  assert_equal summit, firm.clients.where("name = ?", "Summit").to_a
  assert_equal summit, firm.clients.where("name = :name", name: "Summit").to_a
end
test_find_all_with_include_and_conditions() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 808
def test_find_all_with_include_and_conditions
  assert_nothing_raised do
    Developer.all.merge!(joins: :audit_logs, where: { "audit_logs.message" => nil, :name => "Smith" }).to_a
  end
end
test_find_each() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 713
def test_find_each
  firm = companies(:first_firm)

  assert ! firm.clients.loaded?

  assert_queries(4) do
    firm.clients.find_each(batch_size: 1) { |c| assert_equal firm.id, c.firm_id }
  end

  assert ! firm.clients.loaded?
end
test_find_each_with_conditions() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 725
def test_find_each_with_conditions
  firm = companies(:first_firm)

  assert_queries(2) do
    firm.clients.where(name: "Microsoft").find_each(batch_size: 1) do |c|
      assert_equal firm.id, c.firm_id
      assert_equal "Microsoft", c.name
    end
  end

  assert ! firm.clients.loaded?
end
test_find_first() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 759
def test_find_first
  firm = Firm.first
  client2 = Client.find(2)
  assert_equal firm.clients.first, firm.clients.order("id").first
  assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").order("id").first
end
test_find_first_after_reload() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 796
def test_find_first_after_reload
  firm = Firm.first
  collection = firm.clients

  original_object = collection.first
  assert_same original_object, collection.first, "Expected second call to #first to cache the same object"
  collection.reload

  # It should return a different object, since the association has been reloaded
  assert_not_same original_object, collection.first, "Expected #first after #reload to return a new object"
end
test_find_first_after_reset() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 784
def test_find_first_after_reset
  firm = Firm.first
  collection = firm.clients

  original_object = collection.first
  assert_same original_object, collection.first, "Expected second call to #first to cache the same object"
  collection.reset

  # It should return a different object, since the association has been reloaded
  assert_not_same original_object, collection.first, "Expected #first after #reset to return a new object"
end
test_find_first_after_reset_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 773
def test_find_first_after_reset_scope
  firm = Firm.first
  collection = firm.clients

  original_object = collection.first
  assert_same original_object, collection.first, "Expected second call to #first to cache the same object"

  # It should return a different object, since the association has been reloaded
  assert_not_same original_object, firm.clients.first, "Expected #first to return a new object"
end
test_find_first_sanitized() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 766
def test_find_first_sanitized
  firm = Firm.first
  client2 = Client.find(2)
  assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = ?", "Client").first
  assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = :type", type: "Client").first
end
test_find_grouped() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 819
def test_find_grouped
  all_clients_of_firm1 = Client.all.merge!(where: "firm_id = 1").to_a
  grouped_clients_of_firm1 = Client.all.merge!(where: "firm_id = 1", group: "firm_id", select: "firm_id, count(id) as clients_count").to_a
  assert_equal 3, all_clients_of_firm1.size
  assert_equal 1, grouped_clients_of_firm1.size
end
test_find_ids() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 661
def test_find_ids
  firm = Firm.first

  assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }

  client = firm.clients.find(2)
  assert_kind_of Client, client

  client_ary = firm.clients.find([2])
  assert_kind_of Array, client_ary
  assert_equal client, client_ary.first

  client_ary = firm.clients.find(2, 3)
  assert_kind_of Array, client_ary
  assert_equal 2, client_ary.size
  assert_equal client, client_ary.first

  assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
end
test_find_ids_and_inverse_of() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 693
def test_find_ids_and_inverse_of
  force_signal37_to_load_all_clients_of_firm

  assert_predicate companies(:first_firm).clients_of_firm, :loaded?

  firm = companies(:first_firm)
  client = firm.clients_of_firm.find(3)
  assert_kind_of Client, client

  client_ary = firm.clients_of_firm.find([3])
  assert_kind_of Array, client_ary
  assert_equal client, client_ary.first
end
test_find_in_batches() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 738
def test_find_in_batches
  firm = companies(:first_firm)

  assert ! firm.clients.loaded?

  assert_queries(2) do
    firm.clients.find_in_batches(batch_size: 2) do |clients|
      clients.each { |c| assert_equal firm.id, c.firm_id }
    end
  end

  assert ! firm.clients.loaded?
end
test_find_in_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 814
def test_find_in_collection
  assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
  assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
end
test_find_many_with_merged_options() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 546
def test_find_many_with_merged_options
  assert_equal 1, companies(:first_firm).limited_clients.size
  assert_equal 1, companies(:first_firm).limited_clients.to_a.size
  assert_equal 3, companies(:first_firm).limited_clients.limit(nil).to_a.size
end
test_find_one_message_on_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 681
def test_find_one_message_on_primary_key
  firm = Firm.first

  e = assert_raises(ActiveRecord::RecordNotFound) do
    firm.clients.find(0)
  end
  assert_equal 0, e.id
  assert_equal "id", e.primary_key
  assert_equal "Client", e.model
  assert_match (/\ACouldn't find Client with 'id'=0/), e.message
end
test_find_scoped_grouped() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 826
def test_find_scoped_grouped
  assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
  assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
  assert_equal 3, companies(:first_firm).clients_grouped_by_name.size
  assert_equal 3, companies(:first_firm).clients_grouped_by_name.length
end
test_find_scoped_grouped_having() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 833
def test_find_scoped_grouped_having
  assert_equal 1, authors(:david).popular_grouped_posts.length
  assert_equal 0, authors(:mary).popular_grouped_posts.length
end
test_find_should_append_to_association_order() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 552
def test_find_should_append_to_association_order
  ordered_clients = companies(:first_firm).clients_sorted_desc.order("companies.id")
  assert_equal ["id DESC", "companies.id"], ordered_clients.order_values
end
test_finder_bang_method_with_dirty_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 458
def test_finder_bang_method_with_dirty_target
  company = companies(:first_firm)
  new_clients = []
  assert_no_queries(ignore_none: false) do
    new_clients << company.clients_of_firm.build(name: "Another Client")
    new_clients << company.clients_of_firm.build(name: "Another Client II")
    new_clients << company.clients_of_firm.build(name: "Another Client III")
  end

  assert_not company.clients_of_firm.loaded?
  assert_queries(1) do
    assert_same new_clients[0], company.clients_of_firm.third!
    assert_same new_clients[1], company.clients_of_firm.fourth!
    assert_same new_clients[2], company.clients_of_firm.fifth!
    assert_same new_clients[0], company.clients_of_firm.third_to_last!
    assert_same new_clients[1], company.clients_of_firm.second_to_last!
    assert_same new_clients[2], company.clients_of_firm.last!
  end
end
test_finder_method_with_dirty_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 438
def test_finder_method_with_dirty_target
  company = companies(:first_firm)
  new_clients = []
  assert_no_queries(ignore_none: false) do
    new_clients << company.clients_of_firm.build(name: "Another Client")
    new_clients << company.clients_of_firm.build(name: "Another Client II")
    new_clients << company.clients_of_firm.build(name: "Another Client III")
  end

  assert_not company.clients_of_firm.loaded?
  assert_queries(1) do
    assert_same new_clients[0], company.clients_of_firm.third
    assert_same new_clients[1], company.clients_of_firm.fourth
    assert_same new_clients[2], company.clients_of_firm.fifth
    assert_same new_clients[0], company.clients_of_firm.third_to_last
    assert_same new_clients[1], company.clients_of_firm.second_to_last
    assert_same new_clients[2], company.clients_of_firm.last
  end
end
test_finding() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 538
def test_finding
  assert_equal 3, Firm.first.clients.length
end
test_finding_array_compatibility() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 542
def test_finding_array_compatibility
  assert_equal 3, Firm.order(:id).find { |f| f.id > 0 }.clients.length
end
test_finding_default_orders() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 618
def test_finding_default_orders
  assert_equal "Summit", Firm.first.clients.first.name
end
test_finding_using_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 638
def test_finding_using_primary_key
  assert_equal "Summit", Firm.first.clients_using_primary_key.first.name
end
test_finding_with_condition() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 630
def test_finding_with_condition
  assert_equal "Microsoft", Firm.first.clients_like_ms.first.name
end
test_finding_with_condition_hash() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 634
def test_finding_with_condition_hash
  assert_equal "Microsoft", Firm.first.clients_like_ms_with_hash_conditions.first.name
end
test_finding_with_different_class_name_and_order() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 622
def test_finding_with_different_class_name_and_order
  assert_equal "Apex", Firm.first.clients_sorted_desc.first.name
end
test_finding_with_foreign_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 626
def test_finding_with_foreign_key
  assert_equal "Microsoft", Firm.first.clients_of_firm.first.name
end
test_get_ids() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1763
def test_get_ids
  assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], companies(:first_firm).client_ids
end
test_get_ids_for_association_on_new_record_does_not_try_to_find_records() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1796
def test_get_ids_for_association_on_new_record_does_not_try_to_find_records
  Company.columns  # Load schema information so we don't query below
  Contract.columns # if running just this test.

  company = Company.new
  assert_queries(0) do
    company.contract_ids
  end

  assert_equal [], company.contract_ids
end
test_get_ids_for_loaded_associations() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1767
def test_get_ids_for_loaded_associations
  company = companies(:first_firm)
  company.clients.reload
  assert_queries(0) do
    company.client_ids
    company.client_ids
  end
end
test_get_ids_for_ordered_association() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1792
def test_get_ids_for_ordered_association
  assert_equal [companies(:another_first_firm_client).id, companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids
end
test_get_ids_for_through() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1832
def test_get_ids_for_through
  assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
end
test_get_ids_for_unloaded_associations_does_not_load_them() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1776
def test_get_ids_for_unloaded_associations_does_not_load_them
  company = companies(:first_firm)
  assert !company.clients.loaded?
  assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], company.client_ids
  assert !company.clients.loaded?
end
test_get_ids_ignores_include_option() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1788
def test_get_ids_ignores_include_option
  assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
end
test_has_many_build_with_options() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 174
def test_has_many_build_with_options
  college = College.create(name: "UFMT")
  Student.create(active: true, college_id: college.id, name: "Sarah")

  assert_equal college.students, Student.where(active: true, college_id: college.id)
end
test_has_many_through_respects_hash_conditions() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1850
def test_has_many_through_respects_hash_conditions
  assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
  assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
end
test_has_many_without_counter_cache_option() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1106
def test_has_many_without_counter_cache_option
  # Ship has a conventionally named `treasures_count` column, but the counter_cache
  # option is not given on the association.
  ship = Ship.create(name: "Countless", treasures_count: 10)

  assert_not Ship.reflect_on_association(:treasures).has_cached_counter?

  # Count should come from sql count() of treasures rather than treasures_count attribute
  assert_equal ship.treasures.size, 0

  assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
    ship.treasures.create(name: "Gold")
  end

  assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
    ship.treasures.destroy_all
  end
end
test_ids_reader_memoization() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2576
def test_ids_reader_memoization
  car = Car.create!(name: "TofaÅŸ")
  bulb = Bulb.create!(car: car)

  assert_equal [bulb.id], car.bulb_ids
  assert_no_queries { car.bulb_ids }

  bulb2 = car.bulbs.create!

  assert_equal [bulb.id, bulb2.id], car.bulb_ids
  assert_no_queries { car.bulb_ids }
end
test_include_checks_if_record_exists_if_target_not_loaded() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1867
def test_include_checks_if_record_exists_if_target_not_loaded
  firm = companies(:first_firm)
  client = firm.clients.first

  firm.reload
  assert ! firm.clients.loaded?
  assert_queries(1) do
    assert_equal true, firm.clients.include?(client)
  end
  assert ! firm.clients.loaded?
end
test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2139
def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
  post = Post.new
  comment = post.comments.build
  assert_equal true, post.comments.include?(comment)
end
test_include_returns_false_for_non_matching_record_to_verify_scoping() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1879
def test_include_returns_false_for_non_matching_record_to_verify_scoping
  firm = companies(:first_firm)
  client = Client.create!(name: "Not Associated")

  assert ! firm.clients.loaded?
  assert_equal false, firm.clients.include?(client)
end
test_include_uses_array_include_after_loaded() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1855
def test_include_uses_array_include_after_loaded
  firm = companies(:first_firm)
  firm.clients.load_target

  client = firm.clients.first

  assert_no_queries do
    assert firm.clients.loaded?
    assert_equal true, firm.clients.include?(client)
  end
end
test_included_in_collection() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1676
def test_included_in_collection
  assert_equal true, companies(:first_firm).clients.include?(Client.find(2))
end
test_included_in_collection_for_new_records() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1680
def test_included_in_collection_for_new_records
  client = Client.create(name: "Persisted")
  assert_nil client.client_of
  assert_equal false, Firm.new.clients_of_firm.include?(client),
   "includes a client that does not belong to any firm"
end
test_inverse_on_before_validate() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 948
def test_inverse_on_before_validate
  firm = companies(:first_firm)
  assert_queries(1) do
    firm.clients_of_firm << Client.new("name" => "Natural Company")
  end
end
test_joins_with_namespaced_model_should_use_correct_type() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2074
def test_joins_with_namespaced_model_should_use_correct_type
  old = ActiveRecord::Base.store_full_sti_class
  ActiveRecord::Base.store_full_sti_class = true

  firm = Namespaced::Firm.create(name: "Some Company")
  firm.clients.create(name: "Some Client")

  stats = Namespaced::Firm.all.merge!(
    select: "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
    joins: :clients,
    group: "#{Namespaced::Firm.table_name}.id"
  ).find firm.id
  assert_equal 1, stats.num_clients.to_i
ensure
  ActiveRecord::Base.store_full_sti_class = old
end
test_load_target_respects_protected_attributes() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2145
def test_load_target_respects_protected_attributes
  topic = Topic.create!
  reply = topic.replies.create(title: "reply 1")
  reply.approved = false
  reply.save!

  # Save with a different object instance, so the instance that's still held
  # in topic.relies doesn't know about the changed attribute.
  reply2 = Reply.find(reply.id)
  reply2.approved = true
  reply2.save!

  # Force loading the collection from the db. This will merge the existing
  # object (reply) with what gets loaded from the db (which includes the
  # changed approved attribute). approved is a protected attribute, so if mass
  # assignment is used, it won't get updated and will still be false.
  first = topic.replies.to_a.first
  assert_equal reply.id, first.id
  assert_equal true, first.approved?
end
test_loading_association_in_validate_callback_doesnt_affect_persistence() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2589
def test_loading_association_in_validate_callback_doesnt_affect_persistence
  reset_callbacks(:validation, Bulb) do
    Bulb.after_validation { |record| record.car.bulbs.load }

    car = Car.create!(name: "Car")
    bulb = car.bulbs.create!

    assert_equal [bulb], car.bulbs
  end
end
test_merging_with_custom_attribute_writer() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2173
def test_merging_with_custom_attribute_writer
  bulb = Bulb.new(color: "red")
  assert_equal "RED!", bulb.color

  car = Car.create!
  car.bulbs << bulb

  assert_equal "RED!", car.bulbs.to_a.first.color
end
test_modifying_a_through_a_has_many_should_raise() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1836
def test_modifying_a_through_a_has_many_should_raise
  [
    lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
    lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
    lambda { authors(:mary).comments << Comment.create!(body: "Yay", post_id: 424242) },
    lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
  ].each { |block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
end
test_new_aliased_to_build() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 955
def test_new_aliased_to_build
  company = companies(:first_firm)
  new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.new("name" => "Another Client") }
  assert !company.clients_of_firm.loaded?

  assert_equal "Another Client", new_client.name
  assert !new_client.persisted?
  assert_equal new_client, company.clients_of_firm.last
end
test_no_sql_should_be_fired_if_association_already_loaded() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 396
def test_no_sql_should_be_fired_if_association_already_loaded
  Car.create(name: "honda")
  bulbs = Car.first.bulbs
  bulbs.to_a # to load all instances of bulbs

  assert_no_queries do
    bulbs.first()
  end

  assert_no_queries do
    bulbs.second()
  end

  assert_no_queries do
    bulbs.third()
  end

  assert_no_queries do
    bulbs.fourth()
  end

  assert_no_queries do
    bulbs.fifth()
  end

  assert_no_queries do
    bulbs.forty_two()
  end

  assert_no_queries do
    bulbs.third_to_last()
  end

  assert_no_queries do
    bulbs.second_to_last()
  end

  assert_no_queries do
    bulbs.last()
  end
end
test_pushing_association_updates_counter_cache() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1164
def test_pushing_association_updates_counter_cache
  topic = Topic.order("id ASC").first
  reply = Reply.create!

  assert_difference "topic.reload.replies_count", 1 do
    topic.replies << reply
  end
end
test_regular_create_on_has_many_when_parent_is_new_raises() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 889
def test_regular_create_on_has_many_when_parent_is_new_raises
  error = assert_raise(ActiveRecord::RecordNotSaved) do
    firm = Firm.new
    firm.plain_clients.create name: "Whoever"
  end

  assert_equal "You cannot call create unless the parent is saved", error.message
end
test_replace() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2244
def test_replace
  car = Car.create(name: "honda")
  bulb1 = car.bulbs.create
  bulb2 = Bulb.create

  assert_equal [bulb1], car.bulbs
  car.bulbs.replace([bulb2])
  assert_equal [bulb2], car.bulbs
  assert_equal [bulb2], car.reload.bulbs
end
test_replace_failure() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1714
def test_replace_failure
  firm = companies(:first_firm)
  account = Account.new
  orig_accounts = firm.accounts.to_a

  assert !account.valid?
  assert !orig_accounts.empty?
  error = assert_raise ActiveRecord::RecordNotSaved do
    firm.accounts = [account]
  end

  assert_equal orig_accounts, firm.accounts
  assert_equal "Failed to replace accounts because one or more of the " \
               "new records could not be saved.", error.message
end
test_replace_returns_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2255
def test_replace_returns_target
  car = Car.create(name: "honda")
  bulb1 = car.bulbs.create
  bulb2 = car.bulbs.create
  bulb3 = Bulb.create

  assert_equal [bulb1, bulb2], car.bulbs
  result = car.bulbs.replace([bulb3, bulb1])
  assert_equal [bulb1, bulb3], car.bulbs
  assert_equal [bulb1, bulb3], result
end
test_replace_with_less() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1691
def test_replace_with_less
  firm = Firm.first
  firm.clients = [companies(:first_client)]
  assert firm.save, "Could not save firm"
  firm.reload
  assert_equal 1, firm.clients.length
end
test_replace_with_less_and_dependent_nullify() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1699
def test_replace_with_less_and_dependent_nullify
  num_companies = Company.count
  companies(:quails_core).companies = []
  assert_equal num_companies, Company.count
end
test_replace_with_new() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1705
def test_replace_with_new
  firm = Firm.first
  firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
  firm.save
  firm.reload
  assert_equal 2, firm.clients.length
  assert_equal false, firm.clients.include?(:first_client)
end
test_replace_with_same_content() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1730
def test_replace_with_same_content
  firm = Firm.first
  firm.clients = []
  firm.save

  assert_queries(0, ignore_none: true) do
    firm.clients = []
  end

  assert_equal [], firm.send("clients=", [])
end
test_restrict_with_error() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1642
def test_restrict_with_error
  firm = RestrictedWithErrorFirm.create!(name: "restrict")
  firm.companies.create(name: "child")

  assert !firm.companies.empty?

  firm.destroy

  assert !firm.errors.empty?

  assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
  assert RestrictedWithErrorFirm.exists?(name: "restrict")
  assert firm.companies.exists?(name: "child")
end
test_restrict_with_error_with_locale() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1657
def test_restrict_with_error_with_locale
  I18n.backend = I18n::Backend::Simple.new
  I18n.backend.store_translations "en", activerecord: { attributes: { restricted_with_error_firm: { companies: "client companies" } } }
  firm = RestrictedWithErrorFirm.create!(name: "restrict")
  firm.companies.create(name: "child")

  assert !firm.companies.empty?

  firm.destroy

  assert !firm.errors.empty?

  assert_equal "Cannot delete record because dependent client companies exist", firm.errors[:base].first
  assert RestrictedWithErrorFirm.exists?(name: "restrict")
  assert firm.companies.exists?(name: "child")
ensure
  I18n.backend.reload!
end
test_restrict_with_exception() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1632
def test_restrict_with_exception
  firm = RestrictedWithExceptionFirm.create!(name: "restrict")
  firm.companies.create(name: "child")

  assert !firm.companies.empty?
  assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy }
  assert RestrictedWithExceptionFirm.exists?(name: "restrict")
  assert firm.companies.exists?(name: "child")
end
test_select_query_method() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 842
def test_select_query_method
  assert_equal ["id", "body"], posts(:welcome).comments.select(:id, :body).first.attributes.keys
end
test_select_with_block() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 846
def test_select_with_block
  assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id)
end
test_select_with_block_and_dirty_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 850
def test_select_with_block_and_dirty_target
  assert_equal 2, posts(:welcome).comments.select { true }.size
  posts(:welcome).comments.build
  assert_equal 3, posts(:welcome).comments.select { true }.size
end
test_select_without_foreign_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 856
def test_select_without_foreign_key
  assert_equal companies(:first_firm).accounts.first.credit_limit, companies(:first_firm).accounts.select(:credit_limit).first.credit_limit
end
test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2098
def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
  client_association = companies(:first_firm).clients
  assert_equal client_association.new.attributes, client_association.send(:new).attributes
end
test_set_ids_for_association_on_new_record_applies_association_correctly() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1808
def test_set_ids_for_association_on_new_record_applies_association_correctly
  contract_a = Contract.create!
  contract_b = Contract.create!
  Contract.create! # another contract
  company = Company.new(name: "Some Company")

  company.contract_ids = [contract_a.id, contract_b.id]
  assert_equal [contract_a.id, contract_b.id], company.contract_ids
  assert_equal [contract_a, contract_b], company.contracts

  company.save!
  assert_equal company, contract_a.reload.company
  assert_equal company, contract_b.reload.company
end
test_sti_subselect_count() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 124
def test_sti_subselect_count
  tag = Tag.first
  len = Post.tagged_with(tag.id).limit(10).size
  assert_operator len, :>, 0
end
test_taking() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 562
def test_taking
  posts(:other_by_bob).destroy
  assert_equal posts(:misc_by_bob), authors(:bob).posts.take
  assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
  authors(:bob).posts.to_a
  assert_equal posts(:misc_by_bob), authors(:bob).posts.take
  assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
end
test_taking_not_found() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 571
def test_taking_not_found
  authors(:bob).posts.delete_all
  assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
  authors(:bob).posts.to_a
  assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
end
test_taking_with_a_number() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 578
def test_taking_with_a_number
  klass = Class.new(Author) do
    has_many :posts, -> { order(:id) }

    def self.name
      "Author"
    end
  end

  # taking from unloaded Relation
  bob = klass.find(authors(:bob).id)
  new_post = bob.posts.build
  assert_not bob.posts.loaded?
  assert_equal [posts(:misc_by_bob)], bob.posts.take(1)
  assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], bob.posts.take(2)
  assert_equal [posts(:misc_by_bob), posts(:other_by_bob), new_post], bob.posts.take(3)

  # taking from loaded Relation
  bob.posts.load
  assert bob.posts.loaded?
  assert_equal [posts(:misc_by_bob)], bob.posts.take(1)
  assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], bob.posts.take(2)
  assert_equal [posts(:misc_by_bob), posts(:other_by_bob), new_post], bob.posts.take(3)
end
test_taking_with_inverse_of() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 603
def test_taking_with_inverse_of
  interests(:woodsmanship).destroy
  interests(:survival).destroy

  zine = zines(:going_out)
  interest = zine.interests.take
  assert_equal interests(:hunting), interest
  assert_same zine, interest.zine
end
test_three_levels_of_dependence() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1593
def test_three_levels_of_dependence
  topic = Topic.create "title" => "neat and simple"
  reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
  reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"

  assert_nothing_raised { topic.destroy }
end
test_to_a_should_dup_target() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2166
def test_to_a_should_dup_target
  ary    = topics(:first).replies.to_a
  target = topics(:first).replies.target

  assert_not_equal target.object_id, ary.object_id
end
test_transaction_when_deleting_new_record() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1298
def test_transaction_when_deleting_new_record
  assert_no_queries(ignore_none: false) do
    firm = Firm.new
    client = Client.new("name" => "New Client")
    firm.clients_of_firm << client
    firm.clients_of_firm.destroy(client)
  end
end
test_transaction_when_deleting_persisted() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1284
def test_transaction_when_deleting_persisted
  good = Client.new(name: "Good")
  bad  = Client.new(name: "Bad", raise_on_destroy: true)

  companies(:first_firm).clients_of_firm = [good, bad]

  begin
    companies(:first_firm).clients_of_firm.destroy(good, bad)
  rescue Client::RaisedOnDestroy
  end

  assert_equal [good, bad], companies(:first_firm).clients_of_firm.reload
end
test_transactions_when_adding_to_new_record() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 941
def test_transactions_when_adding_to_new_record
  assert_no_queries(ignore_none: false) do
    firm = Firm.new
    firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
  end
end
test_transactions_when_adding_to_persisted() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 929
def test_transactions_when_adding_to_persisted
  good = Client.new(name: "Good")
  bad  = Client.new(name: "Bad", raise_on_save: true)

  begin
    companies(:first_firm).clients_of_firm.concat(good, bad)
  rescue Client::RaisedOnSave
  end

  assert_not_includes companies(:first_firm).clients_of_firm.reload, good
end
test_transactions_when_replacing_on_new_record() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1756
def test_transactions_when_replacing_on_new_record
  assert_no_queries(ignore_none: false) do
    firm = Firm.new
    firm.clients_of_firm = [Client.new("name" => "New Client")]
  end
end
test_transactions_when_replacing_on_persisted() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 1742
def test_transactions_when_replacing_on_persisted
  good = Client.new(name: "Good")
  bad  = Client.new(name: "Bad", raise_on_save: true)

  companies(:first_firm).clients_of_firm = [good]

  begin
    companies(:first_firm).clients_of_firm = [bad]
  rescue Client::RaisedOnSave
  end

  assert_equal [good], companies(:first_firm).clients_of_firm.reload
end
test_update_all_on_association_accessed_before_save() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 642
def test_update_all_on_association_accessed_before_save
  firm = Firm.new(name: "Firm")
  firm.clients << Client.first
  firm.save!
  assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
end
test_update_all_on_association_accessed_before_save_with_explicit_foreign_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 649
def test_update_all_on_association_accessed_before_save_with_explicit_foreign_key
  firm = Firm.new(name: "Firm", id: 100)
  firm.clients << Client.first
  firm.save!
  assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
end
test_update_all_respects_association_scope() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 496
def test_update_all_respects_association_scope
  person = Person.new
  person.first_name = "Naruto"
  person.references << Reference.new
  person.id = 10
  person.references
  person.save!
  assert_equal 1, person.references.update_all(favourite: true)
end
test_with_polymorphic_has_many_with_custom_columns_name() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2189
def test_with_polymorphic_has_many_with_custom_columns_name
  post = Post.create! title: "foo", body: "bar"
  image = Image.create!

  post.images << image

  assert_equal [image], post.images
end

Private Instance Methods

force_signal37_to_load_all_clients_of_firm() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2602
def force_signal37_to_load_all_clients_of_firm
  companies(:first_firm).clients_of_firm.load_target
end
reset_callbacks(kind, klass) { || ... } click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 2606
def reset_callbacks(kind, klass)
  old_callbacks = {}
  old_callbacks[klass] = klass.send("_#{kind}_callbacks").dup
  klass.subclasses.each do |subclass|
    old_callbacks[subclass] = subclass.send("_#{kind}_callbacks").dup
  end
  yield
ensure
  klass.send("_#{kind}_callbacks=", old_callbacks[klass])
  klass.subclasses.each do |subclass|
    subclass.send("_#{kind}_callbacks=", old_callbacks[subclass])
  end
end