diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index ee99d7793115d182fbaeaa3b1e5f9e1a3f048ac1..bbe88cfbd54b79901876aceb6179527ebc8d313a 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -9,7 +9,7 @@ class AssociationsJoinModelTest < Test::Unit::TestCase self.use_transactional_fixtures = false - fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings + fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites def test_has_many assert_equal categories(:general), authors(:david).categories.first @@ -294,6 +294,17 @@ def test_eager_belongs_to_and_has_one_not_singularized end end + def test_self_referential_has_many_through + assert_equal [authors(:mary)], authors(:david).favorite_authors + assert_equal [], authors(:mary).favorite_authors + end + + def test_add_to_self_referential_has_many_through + new_author = Author.create(:name => "Bob") + authors(:david).author_favorites.create :favorite_author => new_author + assert_equal [new_author, authors(:mary)], authors(:david).reload.favorite_authors + end + private # create dynamic Post models to allow different dependency options def find_post_with_dependency(post_id, association, association_name, dependency) diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index e510dd1992e30c7e8802c2fa2ffc3dea765f2da9..df78a7ada8631eff3be1e5daf55bb46c0ae31fc7 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -25,9 +25,12 @@ class Author < ActiveRecord::Base has_many :categorizations has_many :categories, :through => :categorizations - + has_many :nothings, :through => :kateggorisatons, :class_name => 'Category' + has_many :author_favorites + has_many :favorite_authors, :through => :author_favorites, :order => 'name' + belongs_to :author_address attr_accessor :post_log @@ -60,4 +63,9 @@ def raise_exception(object) class AuthorAddress < ActiveRecord::Base has_one :author +end + +class AuthorFavorite < ActiveRecord::Base + belongs_to :author + belongs_to :favorite_author, :class_name => "Author", :foreign_key => 'favorite_author_id' end \ No newline at end of file diff --git a/activerecord/test/fixtures/author_favorites.yml b/activerecord/test/fixtures/author_favorites.yml new file mode 100644 index 0000000000000000000000000000000000000000..e81fdac77891422e847680f34370493ae03e6c08 --- /dev/null +++ b/activerecord/test/fixtures/author_favorites.yml @@ -0,0 +1,4 @@ +david_mary: + id: 1 + author_id: 1 + favorite_author_id: 2 \ No newline at end of file diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 6e8e23a9c4cf849632c6acef9c6665af5aaa8c80..7d10fbcadf872736c170e69c3b09f40935462d5b 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -25,4 +25,8 @@ t.column :author_address_id, :integer end + create_table :author_favorites, :force => true do |t| + t.column :author_id, :integer + t.column :favorite_author_id, :integer + end end \ No newline at end of file