aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/habtm_join_table_test.rb
blob: 85bb7d57bb42433b425cd4e0c96cbf9b1492da7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'cases/helper'

class MyReader < ActiveRecord::Base
  has_and_belongs_to_many :my_books
end

class MyBook < ActiveRecord::Base
  has_and_belongs_to_many :my_readers
end

class HabtmJoinTableTest < ActiveRecord::TestCase
  def setup
    ActiveRecord::Base.connection.create_table :my_books, :force => true do |t|
      t.string :name
    end
    assert ActiveRecord::Base.connection.table_exists?(:my_books)

    ActiveRecord::Base.connection.create_table :my_readers, :force => true do |t|
      t.string :name
    end
    assert ActiveRecord::Base.connection.table_exists?(:my_readers)

    ActiveRecord::Base.connection.create_table :my_books_my_readers, :force => true do |t|
      t.integer :my_book_id
      t.integer :my_reader_id
    end
    assert ActiveRecord::Base.connection.table_exists?(:my_books_my_readers)
  end

  def teardown
    ActiveRecord::Base.connection.drop_table :my_books
    ActiveRecord::Base.connection.drop_table :my_readers
    ActiveRecord::Base.connection.drop_table :my_books_my_readers
  end

  uses_transaction :test_should_not_raise_exception_when_join_table_has_a_primary_key
  def test_should_not_raise_exception_when_join_table_has_a_primary_key
    if ActiveRecord::Base.connection.supports_primary_key?
      # This test is to confirm that this feature is now gone 
      assert MyReader.has_and_belongs_to_many(:my_books).is_a?(ActiveRecord::Reflection::AssociationReflection)
    end
  end
end