diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-27 18:51:32 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-27 18:51:32 +0000 |
commit | 4b639904d7bc4407657767bb963997bc78678d03 (patch) | |
tree | 8819aed22cbcd90b7decee02639a275adbf01572 /activerecord/test | |
parent | cf9be897b81b5cda0282fb3439c11af6032ecedb (diff) | |
download | rails-4b639904d7bc4407657767bb963997bc78678d03.tar.gz rails-4b639904d7bc4407657767bb963997bc78678d03.tar.bz2 rails-4b639904d7bc4407657767bb963997bc78678d03.zip |
Fix has_many :through delete with custom foreign keys. Closes #6466.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8043 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/associations/join_model_test.rb | 18 | ||||
-rw-r--r-- | activerecord/test/fixtures/book.rb | 4 | ||||
-rw-r--r-- | activerecord/test/fixtures/books.yml | 7 | ||||
-rw-r--r-- | activerecord/test/fixtures/citation.rb | 6 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/schema.rb | 9 |
5 files changed, 43 insertions, 1 deletions
diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb index 6b67b15664..0e146f74c4 100644 --- a/activerecord/test/associations/join_model_test.rb +++ b/activerecord/test/associations/join_model_test.rb @@ -9,10 +9,12 @@ require 'fixtures/category' require 'fixtures/categorization' require 'fixtures/vertex' require 'fixtures/edge' +require 'fixtures/book' +require 'fixtures/citation' class AssociationsJoinModelTest < Test::Unit::TestCase self.use_transactional_fixtures = false - fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items + fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books def test_has_many assert authors(:david).categories.include?(categories(:general)) @@ -476,6 +478,20 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal tags, posts(:thinking).tags.push(tags(:general)) end + def test_delete_associate_when_deleting_from_has_many_through_with_nonstandard_id + count = books(:awdr).references.count + references_before = books(:awdr).references + book = Book.create!(:name => 'Getting Real') + book_awdr = books(:awdr) + book_awdr.references << book + assert_equal(count + 1, book_awdr.references(true).size) + + assert_nothing_raised { book_awdr.references.delete(book) } + assert_equal(count, book_awdr.references.size) + assert_equal(count, book_awdr.references(true).size) + assert_equal(references_before.sort, book_awdr.references.sort) + end + def test_delete_associate_when_deleting_from_has_many_through count = posts(:thinking).tags.count tags_before = posts(:thinking).tags diff --git a/activerecord/test/fixtures/book.rb b/activerecord/test/fixtures/book.rb new file mode 100644 index 0000000000..cfd07abddc --- /dev/null +++ b/activerecord/test/fixtures/book.rb @@ -0,0 +1,4 @@ +class Book < ActiveRecord::Base + has_many :citations, :foreign_key => 'book1_id' + has_many :references, :through => :citations, :source => :reference_of, :uniq => true +end diff --git a/activerecord/test/fixtures/books.yml b/activerecord/test/fixtures/books.yml new file mode 100644 index 0000000000..473663ff5b --- /dev/null +++ b/activerecord/test/fixtures/books.yml @@ -0,0 +1,7 @@ +awdr: + id: 1 + name: "Agile Web Development with Rails" + +rfr: + id: 2 + name: "Ruby for Rails" diff --git a/activerecord/test/fixtures/citation.rb b/activerecord/test/fixtures/citation.rb new file mode 100644 index 0000000000..545aa8110d --- /dev/null +++ b/activerecord/test/fixtures/citation.rb @@ -0,0 +1,6 @@ +class Citation < ActiveRecord::Base + belongs_to :reference_of, :class_name => "Book", :foreign_key => :book2_id + + belongs_to :book1, :class_name => "Book", :foreign_key => :book1_id + belongs_to :book2, :class_name => "Book", :foreign_key => :book2_id +end diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 9cadc5bdb8..5bde5126d5 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -290,6 +290,15 @@ ActiveRecord::Schema.define do t.column :developer_id, :integer, :null=>false end + create_table :books, :force => true do |t| + t.column :name, :string + end + + create_table :citations, :force => true do |t| + t.column :book1_id, :integer + t.column :book2_id, :integer + end + create_table :inept_wizards, :force => true do |t| t.column :name, :string, :null => false t.column :city, :string, :null => false |