class HasAndBelongsToManyAssociationsTest

Public Instance Methods

setup_data_for_habtm_case() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 135
def setup_data_for_habtm_case
  ActiveRecord::Base.connection.execute("delete from countries_treaties")

  country = Country.new(name: "India")
  country.country_id = "c1"
  country.save!

  treaty = Treaty.new(name: "peace")
  treaty.treaty_id = "t1"
  country.treaties << treaty
end
test_adding_a_collection() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 257
def test_adding_a_collection
  aredridel = Developer.new("name" => "Aredridel")
  aredridel.save
  aredridel.projects.reload
  aredridel.projects.concat([Project.find(1), Project.find(2)])
  assert_equal 2, aredridel.projects.size
  assert_equal 2, aredridel.projects.reload.size
end
test_adding_from_the_project() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 218
def test_adding_from_the_project
  jamis = Developer.find(2)
  action_controller = Project.find(2)
  action_controller.developers.reload
  assert_equal 1, jamis.projects.size
  assert_equal 1, action_controller.developers.size

  action_controller.developers << jamis

  assert_equal 2, jamis.projects.reload.size
  assert_equal 2, action_controller.developers.size
  assert_equal 2, action_controller.developers.reload.size
end
test_adding_from_the_project_fixed_timestamp() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 232
def test_adding_from_the_project_fixed_timestamp
  jamis = Developer.find(2)
  action_controller = Project.find(2)
  action_controller.developers.reload
  assert_equal 1, jamis.projects.size
  assert_equal 1, action_controller.developers.size
  updated_at = jamis.updated_at

  action_controller.developers << jamis

  assert_equal updated_at, jamis.updated_at
  assert_equal 2, jamis.projects.reload.size
  assert_equal 2, action_controller.developers.size
  assert_equal 2, action_controller.developers.reload.size
end
test_adding_multiple() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 248
def test_adding_multiple
  aredridel = Developer.new("name" => "Aredridel")
  aredridel.save
  aredridel.projects.reload
  aredridel.projects.push(Project.find(1), Project.find(2))
  assert_equal 2, aredridel.projects.size
  assert_equal 2, aredridel.projects.reload.size
end
test_adding_single() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 198
def test_adding_single
  jamis = Developer.find(2)
  jamis.projects.reload # causing the collection to load
  action_controller = Project.find(2)
  assert_equal 1, jamis.projects.size
  assert_equal 1, action_controller.developers.size

  jamis.projects << action_controller

  assert_equal 2, jamis.projects.size
  assert_equal 2, jamis.projects.reload.size
  assert_equal 2, action_controller.developers.reload.size
end
test_adding_type_mismatch() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 212
def test_adding_type_mismatch
  jamis = Developer.find(2)
  assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
  assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
end
test_alternate_database() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 964
def test_alternate_database
  professor = Professor.create(name: "Plum")
  course = Course.create(name: "Forensics")
  assert_equal 0, professor.courses.count
  assert_nothing_raised do
    professor.courses << course
  end
  assert_equal 1, professor.courses.count
end
test_assign_ids() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 777
def test_assign_ids
  developer = Developer.new("name" => "Joe")
  developer.project_ids = projects(:active_record, :action_controller).map(&:id)
  developer.save
  developer.reload
  assert_equal 2, developer.projects.length
  assert_equal [projects(:active_record), projects(:action_controller)].map(&:id).sort, developer.project_ids.sort
end
test_assign_ids_ignoring_blanks() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 786
def test_assign_ids_ignoring_blanks
  developer = Developer.new("name" => "Joe")
  developer.project_ids = [projects(:active_record).id, nil, projects(:action_controller).id, ""]
  developer.save
  developer.reload
  assert_equal 2, developer.projects.length
  assert_equal [projects(:active_record), projects(:action_controller)].map(&:id).sort, developer.project_ids.sort
end
test_association_name_is_the_same_as_join_table_name() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 1011
def test_association_name_is_the_same_as_join_table_name
  user = User.create!
  assert_nothing_raised { user.jobs_pool.clear }
end
test_association_proxy_transaction_method_starts_transaction_in_association_class() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 830
def test_association_proxy_transaction_method_starts_transaction_in_association_class
  assert_called(Post, :transaction) do
    Category.first.posts.transaction do
      # nothing
    end
  end
end
test_association_with_extend_option() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 624
def test_association_with_extend_option
  eponine = DeveloperWithExtendOption.create(name: "Eponine")
  assert_equal "sns", eponine.projects.category
end
test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_create() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 890
def test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_create
  rich_person = RichPerson.new

  treasure = Treasure.new
  treasure.rich_people << rich_person
  treasure.valid?

  assert_equal 1, treasure.rich_people.size
  assert_nil rich_person.first_name, "should not run associated person validation on create when validate: false"
end
test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_update() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 901
def test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_update
  rich_person = RichPerson.create!
  person_first_name = rich_person.first_name
  assert_not_nil person_first_name

  treasure = Treasure.new
  treasure.rich_people << rich_person
  treasure.valid?

  assert_equal 1, treasure.rich_people.size
  assert_equal person_first_name, rich_person.first_name, "should not run associated person validation on update when validate: false"
end
test_associations_with_conditions() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 526
def test_associations_with_conditions
  assert_equal 3, projects(:active_record).developers.size
  assert_equal 1, projects(:active_record).developers_named_david.size
  assert_equal 1, projects(:active_record).developers_named_david_with_hash_conditions.size

  assert_equal developers(:david), projects(:active_record).developers_named_david.find(developers(:david).id)
  assert_equal developers(:david), projects(:active_record).developers_named_david_with_hash_conditions.find(developers(:david).id)
  assert_equal developers(:david), projects(:active_record).salaried_developers.find(developers(:david).id)

  projects(:active_record).developers_named_david.clear
  assert_equal 2, projects(:active_record, :reload).developers.size
end
test_attributes_are_being_set_when_initialized_from_habtm_association_with_multiple_where_clauses() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 858
def test_attributes_are_being_set_when_initialized_from_habtm_association_with_multiple_where_clauses
  new_developer = projects(:action_controller).developers.where(name: "Marcelo").where(salary: 90_000).build
  assert_equal new_developer.name, "Marcelo"
  assert_equal new_developer.salary, 90_000
end
test_attributes_are_being_set_when_initialized_from_habtm_association_with_where_clause() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 853
def test_attributes_are_being_set_when_initialized_from_habtm_association_with_where_clause
  new_developer = projects(:action_controller).developers.where(name: "Marcelo").build
  assert_equal new_developer.name, "Marcelo"
end
test_build() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 317
def test_build
  devel = Developer.find(1)
  proj = assert_no_queries(ignore_none: false) { devel.projects.build("name" => "Projekt") }
  assert !devel.projects.loaded?

  assert_equal devel.projects.last, proj
  assert devel.projects.loaded?

  assert !proj.persisted?
  devel.save
  assert proj.persisted?
  assert_equal devel.projects.last, proj
  assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
end
test_build_by_new_record() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 347
def test_build_by_new_record
  devel = Developer.new(name: "Marcel", salary: 75000)
  devel.projects.build(name: "Make bed")
  proj2 = devel.projects.build(name: "Lie in it")
  assert_equal devel.projects.last, proj2
  assert !proj2.persisted?
  devel.save
  assert devel.persisted?
  assert proj2.persisted?
  assert_equal devel.projects.last, proj2
  assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated
end
test_caching_of_columns() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 838
def test_caching_of_columns
  david = Developer.find(1)
  # clear cache possibly created by other tests
  david.projects.reset_column_information

  assert_queries(:any) { david.projects.columns }
  assert_no_queries { david.projects.columns }

  ## and again to verify that reset_column_information clears the cache correctly
  david.projects.reset_column_information

  assert_queries(:any) { david.projects.columns }
  assert_no_queries { david.projects.columns }
end
test_consider_type() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 651
def test_consider_type
  developer = Developer.first
  special_project = SpecialProject.create("name" => "Special Project")

  other_project = developer.projects.first
  developer.special_projects << special_project
  developer.reload

  assert_includes developer.projects, special_project
  assert_includes developer.special_projects, special_project
  assert_not_includes developer.special_projects, other_project
end
test_count() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 825
def test_count
  david = Developer.find(1)
  assert_equal 2, david.projects.count
end
test_create() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 360
def test_create
  devel = Developer.find(1)
  proj = devel.projects.create("name" => "Projekt")
  assert !devel.projects.loaded?

  assert_equal devel.projects.last, proj
  assert !devel.projects.loaded?

  assert proj.persisted?
  assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
end
test_creation_respects_hash_condition() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 372
def test_creation_respects_hash_condition
  # in Oracle '' is saved as null therefore need to save ' ' in not null column
  post = categories(:general).post_with_conditions.build(body: " ")

  assert        post.save
  assert_equal  "Yet Another Testing Title", post.title

  # in Oracle '' is saved as null therefore need to save ' ' in not null column
  another_post = categories(:general).post_with_conditions.create(body: " ")

  assert        another_post.persisted?
  assert_equal  "Yet Another Testing Title", another_post.title
end
test_custom_join_table() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 914
def test_custom_join_table
  assert_equal "edges", Vertex.reflect_on_association(:sources).join_table
end
test_deleting() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 418
def test_deleting
  david = Developer.find(1)
  active_record = Project.find(1)
  david.projects.reload
  assert_equal 2, david.projects.size
  assert_equal 3, active_record.developers.size

  david.projects.delete(active_record)

  assert_equal 1, david.projects.size
  assert_equal 1, david.projects.reload.size
  assert_equal 2, active_record.developers.reload.size
end
test_deleting_all() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 440
def test_deleting_all
  david = Developer.find(1)
  david.projects.reload
  david.projects.clear
  assert_equal 0, david.projects.size
  assert_equal 0, david.projects.reload.size
end
test_deleting_array() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 432
def test_deleting_array
  david = Developer.find(1)
  david.projects.reload
  david.projects.delete(Project.all.to_a)
  assert_equal 0, david.projects.size
  assert_equal 0, david.projects.reload.size
end
test_destroy_all() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 490
def test_destroy_all
  david = Developer.find(1)
  david.projects.reload
  assert !david.projects.empty?

  assert_no_difference "Project.count" do
    david.projects.destroy_all
  end

  join_records = Developer.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = #{david.id}")
  assert join_records.empty?

  assert david.projects.empty?
  assert david.projects.reload.empty?
end
test_destroy_associations_destroys_multiple_associations() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 506
def test_destroy_associations_destroys_multiple_associations
  george = parrots(:george)
  assert !george.pirates.empty?
  assert !george.treasures.empty?

  assert_no_difference "Pirate.count" do
    assert_no_difference "Treasure.count" do
      george.destroy_associations
    end
  end

  join_records = Parrot.connection.select_all("SELECT * FROM parrots_pirates WHERE parrot_id = #{george.id}")
  assert join_records.empty?
  assert george.pirates.reload.empty?

  join_records = Parrot.connection.select_all("SELECT * FROM parrots_treasures WHERE parrot_id = #{george.id}")
  assert join_records.empty?
  assert george.treasures.reload.empty?
end
test_destroying() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 456
def test_destroying
  david = Developer.find(1)
  project = Project.find(1)
  david.projects.reload
  assert_equal 2, david.projects.size
  assert_equal 3, project.developers.size

  assert_no_difference "Project.count" do
    david.projects.destroy(project)
  end

  join_records = Developer.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = #{david.id} AND project_id = #{project.id}")
  assert join_records.empty?

  assert_equal 1, david.reload.projects.size
  assert_equal 1, david.projects.reload.size
end
test_destroying_many() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 474
def test_destroying_many
  david = Developer.find(1)
  david.projects.reload
  projects = Project.all.to_a

  assert_no_difference "Project.count" do
    david.projects.destroy(*projects)
  end

  join_records = Developer.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = #{david.id}")
  assert join_records.empty?

  assert_equal 0, david.reload.projects.size
  assert_equal 0, david.projects.reload.size
end
test_destruction_does_not_error_without_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 870
def test_destruction_does_not_error_without_primary_key
  redbeard = pirates(:redbeard)
  george = parrots(:george)
  redbeard.parrots << george
  assert_equal 2, george.pirates.count
  Pirate.includes(:parrots).where(parrot: redbeard.parrot).find(redbeard.id).destroy
  assert_equal 1, george.pirates.count
  assert_equal [], Pirate.where(id: redbeard.id)
end
test_distinct_after_the_fact() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 386
def test_distinct_after_the_fact
  dev = developers(:jamis)
  dev.projects << projects(:active_record)
  dev.projects << projects(:active_record)

  assert_equal 3, dev.projects.size
  assert_equal 1, dev.projects.uniq.size
end
test_distinct_before_the_fact() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 395
def test_distinct_before_the_fact
  projects(:active_record).developers << developers(:jamis)
  projects(:active_record).developers << developers(:david)
  assert_equal 3, projects(:active_record, :reload).developers.size
end
test_distinct_option_prevents_duplicate_push() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 401
def test_distinct_option_prevents_duplicate_push
  project = projects(:active_record)
  project.developers << developers(:jamis)
  project.developers << developers(:david)
  assert_equal 3, project.developers.size

  project.developers << developers(:david)
  project.developers << developers(:jamis)
  assert_equal 3, project.developers.size
end
test_distinct_when_association_already_loaded() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 412
def test_distinct_when_association_already_loaded
  project = projects(:active_record)
  project.developers << [ developers(:jamis), developers(:david), developers(:jamis), developers(:david) ]
  assert_equal 3, Project.includes(:developers).find(project.id).developers.size
end
test_dynamic_find_all_should_respect_readonly_access() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 600
def test_dynamic_find_all_should_respect_readonly_access
  projects(:active_record).readonly_developers.each { |d| assert_raise(ActiveRecord::ReadOnlyRecord) { d.save!  } if d.valid? }
  projects(:active_record).readonly_developers.each(&:readonly?)
end
test_dynamic_find_should_respect_association_include() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 819
def test_dynamic_find_should_respect_association_include
  # SQL error in sort clause if :include is not included
  # due to Unknown column 'authors.id'
  assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by_title("Welcome to the weblog")
end
test_dynamic_find_should_respect_association_order() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 587
def test_dynamic_find_should_respect_association_order
  # Developers are ordered 'name DESC, id DESC'
  high_id_jamis = projects(:active_record).developers.create(name: "Jamis")

  assert_equal high_id_jamis, projects(:active_record).developers.merge(where: "name = 'Jamis'").first
  assert_equal high_id_jamis, projects(:active_record).developers.find_by_name("Jamis")
end
test_find_grouped() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 739
def test_find_grouped
  all_posts_from_category1 = Post.all.merge!(where: "category_id = 1", joins: :categories).to_a
  grouped_posts_of_category1 = Post.all.merge!(where: "category_id = 1", group: "author_id", select: "count(posts.id) as posts_count", joins: :categories).to_a
  assert_equal 5, all_posts_from_category1.size
  assert_equal 2, grouped_posts_of_category1.size
end
test_find_in_association() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 539
def test_find_in_association
  # Using sql
  assert_equal developers(:david), projects(:active_record).developers.find(developers(:david).id), "SQL find"

  # Using ruby
  active_record = projects(:active_record)
  active_record.developers.reload
  assert_equal developers(:david), active_record.developers.find(developers(:david).id), "Ruby find"
end
test_find_in_association_with_options() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 617
def test_find_in_association_with_options
  developers = projects(:active_record).developers.to_a
  assert_equal 3, developers.size

  assert_equal developers(:poor_jamis), projects(:active_record).developers.where("salary < 10000").first
end
test_find_scoped_grouped() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 746
def test_find_scoped_grouped
  assert_equal 5, categories(:general).posts_grouped_by_title.to_a.size
  assert_equal 1, categories(:technology).posts_grouped_by_title.to_a.size
end
test_find_scoped_grouped_having() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 751
def test_find_scoped_grouped_having
  assert_equal 2, projects(:active_record).well_paid_salary_groups.to_a.size
  assert projects(:active_record).well_paid_salary_groups.all? { |g| g.salary > 10000 }
end
test_find_should_append_to_association_order() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 595
def test_find_should_append_to_association_order
  ordered_developers = projects(:active_record).developers.order("projects.id")
  assert_equal ["developers.name desc, developers.id desc", "projects.id"], ordered_developers.order_values
end
test_find_with_merged_options() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 581
def test_find_with_merged_options
  assert_equal 1, projects(:active_record).limited_developers.size
  assert_equal 1, projects(:active_record).limited_developers.to_a.size
  assert_equal 3, projects(:active_record).limited_developers.limit(nil).to_a.size
end
test_get_ids() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 756
def test_get_ids
  assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort
  assert_equal [projects(:active_record).id], developers(:jamis).project_ids
end
test_get_ids_for_loaded_associations() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 761
def test_get_ids_for_loaded_associations
  developer = developers(:david)
  developer.projects.reload
  assert_queries(0) do
    developer.project_ids
    developer.project_ids
  end
end
test_get_ids_for_unloaded_associations_does_not_load_them() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 770
def test_get_ids_for_unloaded_associations_does_not_load_them
  developer = developers(:david)
  assert !developer.projects.loaded?
  assert_equal projects(:active_record, :action_controller).map(&:id).sort, developer.project_ids.sort
  assert !developer.projects.loaded?
end
test_habtm_adding_before_save() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 266
def test_habtm_adding_before_save
  no_of_devels = Developer.count
  no_of_projects = Project.count
  aredridel = Developer.new("name" => "Aredridel")
  aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")])
  assert !aredridel.persisted?
  assert !p.persisted?
  assert aredridel.save
  assert aredridel.persisted?
  assert_equal no_of_devels + 1, Developer.count
  assert_equal no_of_projects + 1, Project.count
  assert_equal 2, aredridel.projects.size
  assert_equal 2, aredridel.projects.reload.size
end
test_habtm_collection_size_from_build() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 300
def test_habtm_collection_size_from_build
  devel = Developer.create("name" => "Fred Wu")
  devel.projects << Project.create("name" => "Grimetime")
  devel.projects.build

  assert_equal 2, devel.projects.size
end
test_habtm_collection_size_from_params() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 308
def test_habtm_collection_size_from_params
  devel = Developer.new(
    projects_attributes: {
      "0" => {}
    })

  assert_equal 1, devel.projects.size
end
test_habtm_distinct_order_preserved() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 295
def test_habtm_distinct_order_preserved
  assert_equal developers(:poor_jamis, :jamis, :david), projects(:active_record).non_unique_developers
  assert_equal developers(:poor_jamis, :jamis, :david), projects(:active_record).developers
end
test_habtm_respects_select() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 690
def test_habtm_respects_select
  categories(:technology).select_testing_posts.reload.each do |o|
    assert_respond_to o, :correctness_marker
  end
  assert_respond_to categories(:technology).select_testing_posts.first, :correctness_marker
end
test_habtm_respects_select_query_method() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 701
def test_habtm_respects_select_query_method
  assert_equal ["id"], developers(:david).projects.select(:id).first.attributes.keys
end
test_habtm_saving_multiple_relationships() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 281
def test_habtm_saving_multiple_relationships
  new_project = Project.new("name" => "Grimetime")
  amount_of_developers = 4
  developers = (0...amount_of_developers).collect { |i| Developer.create(name: "JME #{i}") }.reverse

  new_project.developer_ids = [developers[0].id, developers[1].id]
  new_project.developers_with_callback_ids = [developers[2].id, developers[3].id]
  assert new_project.save

  new_project.reload
  assert_equal amount_of_developers, new_project.developers.size
  assert_equal developers, new_project.developers
end
test_habtm_scope_can_unscope() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 974
def test_habtm_scope_can_unscope
  project = ProjectUnscopingDavidDefaultScope.new
  project.save!

  developer = LazyBlockDeveloperCalledDavid.new(name: "Not David")
  developer.save!
  project.developers << developer

  projects = ProjectUnscopingDavidDefaultScope.includes(:developers).where(id: project.id)
  assert_equal 1, projects.first.developers.size
end
test_habtm_selects_all_columns_by_default() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 697
def test_habtm_selects_all_columns_by_default
  assert_equal Project.column_names.sort, developers(:david).projects.first.attributes.keys.sort
end
test_habtm_with_reflection_using_class_name_and_fixtures() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 942
def test_habtm_with_reflection_using_class_name_and_fixtures
  assert_not_nil Developer._reflections["shared_computers"]
  # Checking the fixture for named association is important here, because it's the only way
  # we've been able to reproduce this bug
  assert_not_nil File.read(File.expand_path("../../fixtures/developers.yml", __dir__)).index("shared_computers")
  assert_equal developers(:david).shared_computers.first, computers(:laptop)
end
test_has_and_belongs_to_many() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 186
def test_has_and_belongs_to_many
  david = Developer.find(1)

  assert !david.projects.empty?
  assert_equal 2, david.projects.size

  active_record = Project.find(1)
  assert !active_record.developers.empty?
  assert_equal 3, active_record.developers.size
  assert_includes active_record.developers, david
end
test_has_and_belongs_to_many_associations_on_new_records_use_null_relations() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 880
def test_has_and_belongs_to_many_associations_on_new_records_use_null_relations
  projects = Developer.new.projects
  assert_no_queries(ignore_none: false) do
    assert_equal [], projects
    assert_equal [], projects.where(title: "omg")
    assert_equal [], projects.pluck(:title)
    assert_equal 0, projects.count
  end
end
test_has_and_belongs_to_many_in_a_namespaced_model_pointing_to_a_namespaced_model() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 918
def test_has_and_belongs_to_many_in_a_namespaced_model_pointing_to_a_namespaced_model
  magazine = Publisher::Magazine.create
  article = Publisher::Article.create
  magazine.articles << article
  magazine.save

  assert_includes magazine.articles, article
end
test_has_and_belongs_to_many_in_a_namespaced_model_pointing_to_a_non_namespaced_model() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 927
def test_has_and_belongs_to_many_in_a_namespaced_model_pointing_to_a_non_namespaced_model
  article = Publisher::Article.create
  tag = Tag.create
  article.tags << tag
  article.save

  assert_includes article.tags, tag
end
test_has_and_belongs_to_many_is_useable_with_belongs_to_required_by_default() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 1005
def test_has_and_belongs_to_many_is_useable_with_belongs_to_required_by_default
  assert_difference "Project.first.developers_required_by_default.size", 1 do
    Project.first.developers_required_by_default.create!(name: "Sean", salary: 50000)
  end
end
test_has_and_belongs_to_many_while_partial_writes_false() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 1016
def test_has_and_belongs_to_many_while_partial_writes_false
  begin
    original_partial_writes = ActiveRecord::Base.partial_writes
    ActiveRecord::Base.partial_writes = false
    developer = Developer.new(name: "Mehmet Emin İNAÇ")
    developer.projects << Project.new(name: "Bounty")

    assert developer.save
  ensure
    ActiveRecord::Base.partial_writes = original_partial_writes
  end
end
test_has_and_belongs_to_many_with_belongs_to() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 1029
def test_has_and_belongs_to_many_with_belongs_to
  sink = Sink.create! kitchen: Kitchen.new, sources: [Source.new]
  assert_equal 1, sink.sources.count
end
test_has_many_through_polymorphic_has_manys_works() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 803
def test_has_many_through_polymorphic_has_manys_works
  assert_equal [10, 20].to_set, pirates(:redbeard).treasure_estimates.map(&:price).to_set
end
test_include_checks_if_record_exists_if_target_not_loaded() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 561
def test_include_checks_if_record_exists_if_target_not_loaded
  project = projects(:active_record)
  developer = project.developers.first

  project.reload
  assert ! project.developers.loaded?
  assert_queries(1) do
    assert_includes project.developers, developer
  end
  assert ! project.developers.loaded?
end
test_include_method_in_has_and_belongs_to_many_association_should_return_true_for_instance_added_with_build() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 864
def test_include_method_in_has_and_belongs_to_many_association_should_return_true_for_instance_added_with_build
  project = Project.new
  developer = project.developers.build
  assert_includes project.developers, developer
end
test_include_returns_false_for_non_matching_record_to_verify_scoping() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 573
def test_include_returns_false_for_non_matching_record_to_verify_scoping
  project = projects(:active_record)
  developer = Developer.create name: "Bryan", salary: 50_000

  assert ! project.developers.loaded?
  assert ! project.developers.include?(developer)
end
test_include_uses_array_include_after_loaded() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 549
def test_include_uses_array_include_after_loaded
  project = projects(:active_record)
  project.developers.load_target

  developer = project.developers.first

  assert_no_queries(ignore_none: false) do
    assert project.developers.loaded?
    assert_includes project.developers, developer
  end
end
test_join_table_alias() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 705
def test_join_table_alias
  # FIXME: `references` has no impact on the aliases generated for the join
  # query.  The fact that we pass `:developers_projects_join` to `references`
  # and that the SQL string contains `developers_projects_join` is merely a
  # coincidence.
  assert_equal(
    3,
    Developer.references(:developers_projects_join).merge(
      includes: { projects: :developers },
      where: "projects_developers_projects_join.joined_on IS NOT NULL"
    ).to_a.size
  )
end
test_join_table_composite_primary_key_should_not_warn() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 173
def test_join_table_composite_primary_key_should_not_warn
  country = Country.new(name: "India")
  country.country_id = "c1"
  country.save!

  treaty = Treaty.new(name: "peace")
  treaty.treaty_id = "t1"
  warning = capture(:stderr) do
    country.treaties << treaty
  end
  assert_no_match(/WARNING: Active Record does not support composite primary key\./, warning)
end
test_join_with_group() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 719
def test_join_with_group
  # FIXME: `references` has no impact on the aliases generated for the join
  # query.  The fact that we pass `:developers_projects_join` to `references`
  # and that the SQL string contains `developers_projects_join` is merely a
  # coincidence.
  group = Developer.columns.inject([]) do |g, c|
    g << "developers.#{c.name}"
    g << "developers_projects_2.#{c.name}"
  end
  Project.columns.each { |c| group << "projects.#{c.name}" }

  assert_equal(
    3,
    Developer.references(:developers_projects_join).merge(
      includes: { projects: :developers }, where: "projects_developers_projects_join.joined_on IS NOT NULL",
      group: group.join(",")
    ).to_a.size
  )
end
test_marshal_dump() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 147
def test_marshal_dump
  post = posts :welcome
  preloaded = Post.includes(:categories).find post.id
  assert_equal preloaded, Marshal.load(Marshal.dump(preloaded))
end
test_new_aliased_to_build() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 332
def test_new_aliased_to_build
  devel = Developer.find(1)
  proj = assert_no_queries(ignore_none: false) { devel.projects.new("name" => "Projekt") }
  assert !devel.projects.loaded?

  assert_equal devel.projects.last, proj
  assert devel.projects.loaded?

  assert !proj.persisted?
  devel.save
  assert proj.persisted?
  assert_equal devel.projects.last, proj
  assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
end
test_new_with_values_in_collection() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 605
def test_new_with_values_in_collection
  jamis = DeveloperForProjectWithAfterCreateHook.find_by_name("Jamis")
  david = DeveloperForProjectWithAfterCreateHook.find_by_name("David")
  project = ProjectWithAfterCreateHook.new(name: "Cooking with Bertie")
  project.developers << jamis
  project.save!
  project.reload

  assert_includes project.developers, jamis
  assert_includes project.developers, david
end
test_preloaded_associations_size() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 986
def test_preloaded_associations_size
  assert_equal Project.first.salaried_developers.size,
    Project.preload(:salaried_developers).first.salaried_developers.size

  assert_equal Project.includes(:salaried_developers).references(:salaried_developers).first.salaried_developers.size,
    Project.preload(:salaried_developers).first.salaried_developers.size

  # Nested HATBM
  first_project = Developer.first.projects.first
  preloaded_first_project =
    Developer.preload(projects: :salaried_developers).
      first.
      projects.
      detect { |p| p.id == first_project.id }

  assert preloaded_first_project.salaried_developers.loaded?, true
  assert_equal first_project.salaried_developers.size, preloaded_first_project.salaried_developers.size
end
test_proper_usage_of_primary_keys_and_join_table() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 163
def test_proper_usage_of_primary_keys_and_join_table
  setup_data_for_habtm_case

  assert_equal "country_id", Country.primary_key
  assert_equal "treaty_id", Treaty.primary_key

  country = Country.first
  assert_equal 1, country.treaties.count
end
test_redefine_habtm() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 936
def test_redefine_habtm
  child = SubDeveloper.new("name" => "Aredridel")
  child.special_projects << SpecialProject.new("name" => "Special Project")
  assert child.save, "child object should be saved"
end
test_removing_associations_on_destroy() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 448
def test_removing_associations_on_destroy
  david = DeveloperWithBeforeDestroyRaise.find(1)
  assert !david.projects.empty?
  david.destroy
  assert david.projects.empty?
  assert DeveloperWithBeforeDestroyRaise.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = 1").empty?
end
test_replace_on_new_object() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 644
def test_replace_on_new_object
  new_developer = Developer.new("name" => "Matz")
  new_developer.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")]
  new_developer.save
  assert_equal 2, new_developer.projects.length
end
test_replace_with_less() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 629
def test_replace_with_less
  david = developers(:david)
  david.projects = [projects(:action_controller)]
  assert david.save
  assert_equal 1, david.projects.length
end
test_replace_with_new() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 636
def test_replace_with_new
  david = developers(:david)
  david.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")]
  david.save
  assert_equal 2, david.projects.length
  assert_not_includes david.projects, projects(:active_record)
end
test_scoped_find_on_through_association_doesnt_return_read_only_records() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 795
def test_scoped_find_on_through_association_doesnt_return_read_only_records
  tag = Post.find(1).tags.find_by_name("General")

  assert_nothing_raised do
    tag.save!
  end
end
test_should_property_quote_string_primary_keys() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 153
def test_should_property_quote_string_primary_keys
  setup_data_for_habtm_case

  con = ActiveRecord::Base.connection
  sql = "select * from countries_treaties"
  record = con.select_rows(sql).last
  assert_equal "c1", record[0]
  assert_equal "t1", record[1]
end
test_symbol_join_table() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 664
def test_symbol_join_table
  developer = Developer.first
  sp = developer.sym_special_projects.create("name" => "omg")
  developer.reload
  assert_includes developer.sym_special_projects, sp
end
test_symbols_as_keys() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 807
def test_symbols_as_keys
  developer = DeveloperWithSymbolsForKeys.new(name: "David")
  project = ProjectWithSymbolsForKeys.new(name: "Quails Testing")
  project.developers << developer
  project.save!

  assert_equal 1, project.developers.size
  assert_equal 1, developer.projects.size
  assert_equal developer, project.developers.first
  assert_equal project, developer.projects.first
end
test_update_attributes_after_push_without_duplicate_join_table_rows() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 671
  def test_update_attributes_after_push_without_duplicate_join_table_rows
    developer = Developer.new("name" => "Kano")
    project = SpecialProject.create("name" => "Special Project")
    assert developer.save
    developer.projects << project
    developer.update_columns("name" => "Bruza")
    assert_equal 1, Developer.connection.select_value(<<-end_sql).to_i
      SELECT count(*) FROM developers_projects
      WHERE project_id = #{project.id}
      AND developer_id = #{developer.id}
    end_sql
  end
test_updating_attributes_on_non_rich_associations() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 684
def test_updating_attributes_on_non_rich_associations
  welcome = categories(:technology).posts.first
  welcome.title = "Something else"
  assert welcome.save!
end
test_with_constant_class_name() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 957
def test_with_constant_class_name
  assert_nothing_raised do
    developer = DeveloperWithConstantClassName.new
    developer.projects
  end
end
test_with_symbol_class_name() click to toggle source
# File activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb, line 950
def test_with_symbol_class_name
  assert_nothing_raised do
    developer = DeveloperWithSymbolClassName.new
    developer.projects
  end
end