class HasManyAssociationsTestPrimaryKeys

Public Instance Methods

test_association_primary_key_on_new_record_should_fetch_with_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 69
def test_association_primary_key_on_new_record_should_fetch_with_query
  author = Author.new(name: "David")
  assert !author.essays.loaded?

  assert_queries 1 do
    assert_equal 1, author.essays.size
  end

  assert_equal Essay.where(writer_id: "David"), author.essays
end
test_blank_custom_primary_key_on_new_record_should_not_run_queries() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 104
def test_blank_custom_primary_key_on_new_record_should_not_run_queries
  author = Author.new
  assert !author.essays.loaded?

  assert_queries 0 do
    assert_equal 0, author.essays.size
  end
end
test_custom_primary_key_on_new_record_should_fetch_with_query() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 58
def test_custom_primary_key_on_new_record_should_fetch_with_query
  subscriber = Subscriber.new(nick: "webster132")
  assert !subscriber.subscriptions.loaded?

  assert_queries 1 do
    assert_equal 2, subscriber.subscriptions.size
  end

  assert_equal Subscription.where(subscriber_id: "webster132"), subscriber.subscriptions
end
test_has_many_assignment_with_custom_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 96
def test_has_many_assignment_with_custom_primary_key
  david = people(:david)

  assert_equal ["A Modest Proposal"], david.essays.map(&:name)
  david.essays = [Essay.create!(name: "Remote Work")]
  assert_equal ["Remote Work"], david.essays.map(&:name)
end
test_has_many_custom_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 80
def test_has_many_custom_primary_key
  david = authors(:david)
  assert_equal Essay.where(writer_id: "David"), david.essays
end
test_ids_on_loaded_association_with_custom_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 90
def test_ids_on_loaded_association_with_custom_primary_key
  david = people(:david)
  david.essays.load
  assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
end
test_ids_on_unloaded_association_with_custom_primary_key() click to toggle source
# File activerecord/test/cases/associations/has_many_associations_test.rb, line 85
def test_ids_on_unloaded_association_with_custom_primary_key
  david = people(:david)
  assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
end