public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/JackDanger/rails.git
add self referential has_many :through example [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4006 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
Mon Mar 20 17:07:16 -0800 2006
commit  c8470f8a5b74eefd498397a8effdf37b4dc28674
tree    70162f5690e6f0a757fb5b7f9d85e1acb818fd0a
parent  d15550aeccd5c10d7c734c523116e3da9f4b13e8
...
9
10
11
12
 
13
14
15
...
294
295
296
 
 
 
 
 
 
 
 
 
 
 
297
298
299
...
9
10
11
 
12
13
14
15
...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
0
@@ -9,7 +9,7 @@ require 'fixtures/categorization'
0
 
0
 class AssociationsJoinModelTest < Test::Unit::TestCase
0
   self.use_transactional_fixtures = false
0
- fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings
0
+ fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites
0
 
0
   def test_has_many
0
     assert_equal categories(:general), authors(:david).categories.first
0
@@ -294,6 +294,17 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ def test_self_referential_has_many_through
0
+ assert_equal [authors(:mary)], authors(:david).favorite_authors
0
+ assert_equal [], authors(:mary).favorite_authors
0
+ end
0
+
0
+ def test_add_to_self_referential_has_many_through
0
+ new_author = Author.create(:name => "Bob")
0
+ authors(:david).author_favorites.create :favorite_author => new_author
0
+ assert_equal [new_author, authors(:mary)], authors(:david).reload.favorite_authors
0
+ end
0
+
0
   private
0
     # create dynamic Post models to allow different dependency options
0
     def find_post_with_dependency(post_id, association, association_name, dependency)
...
25
26
27
28
 
29
30
 
 
 
31
32
33
...
60
61
62
 
 
 
 
 
63
64
...
25
26
27
 
28
29
30
31
32
33
34
35
36
...
63
64
65
66
67
68
69
70
71
72
0
@@ -25,9 +25,12 @@ class Author < ActiveRecord::Base
0
 
0
   has_many :categorizations
0
   has_many :categories, :through => :categorizations
0
-
0
+
0
   has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
0
 
0
+ has_many :author_favorites
0
+ has_many :favorite_authors, :through => :author_favorites, :order => 'name'
0
+
0
   belongs_to :author_address
0
 
0
   attr_accessor :post_log
0
@@ -60,4 +63,9 @@ end
0
 
0
 class AuthorAddress < ActiveRecord::Base
0
   has_one :author
0
+end
0
+
0
+class AuthorFavorite < ActiveRecord::Base
0
+ belongs_to :author
0
+ belongs_to :favorite_author, :class_name => "Author", :foreign_key => 'favorite_author_id'
0
 end
0
\ No newline at end of file
...
25
26
27
 
 
 
 
28
29
...
25
26
27
28
29
30
31
32
33
0
@@ -25,4 +25,8 @@ ActiveRecord::Schema.define do
0
     t.column :author_address_id, :integer
0
   end
0
 
0
+ create_table :author_favorites, :force => true do |t|
0
+ t.column :author_id, :integer
0
+ t.column :favorite_author_id, :integer
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.