Permalink
Browse files

Eager loading of dependent has_one associations won't delete the asso…

…ciation #1212. Also, starting to refactor the tests to make them speedier, with optional support for transactional fixtures.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1398 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f6ec9e3 commit 7f9ffb2ebf1a0230368f54a6372cb7196c90177e @jamis jamis committed Jun 10, 2005
View
@@ -1,5 +1,7 @@
*SVN*
+* Eager loading of dependent has_one associations won't delete the association #1212
+
* Added a second parameter to the build and create method for has_one that controls whether the existing association should be replaced (which means nullifying its foreign key as well). By default this is true, but false can be passed to prevent it.
* Using transactional fixtures now causes the data to be loaded only once.
@@ -719,7 +719,7 @@ def find_with_associations(options = {})
next unless row[primary_key_table[reflection.table_name]]
record.send(
- "#{reflection.name}=",
+ "set_#{reflection.name}_target",
reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
)
end
@@ -19,3 +19,6 @@ def create_fixtures(*table_names)
end
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
+#Test::Unit::TestCase.use_instantiated_fixtures = false
+#Test::Unit::TestCase.use_transactional_fixtures = (ENV['AR_TX_FIXTURES'] == "yes")
+
@@ -3,60 +3,69 @@
require 'fixtures/comment'
require 'fixtures/author'
require 'fixtures/category'
+require 'fixtures/company'
class EagerAssociationTest < Test::Unit::TestCase
- fixtures :posts, :comments, :authors, :categories, :categories_posts
+ fixtures :posts, :comments, :authors, :categories, :categories_posts,
+ :companies, :accounts
def test_loading_with_one_association
posts = Post.find(:all, :include => :comments)
assert_equal 2, posts.first.comments.size
- assert posts.first.comments.include?(@greetings)
+ assert posts.first.comments.include?(comments(:greetings))
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
assert_equal 2, post.comments.size
- assert post.comments.include?(@greetings)
+ assert post.comments.include?(comments(:greetings))
end
def test_with_ordering
posts = Post.find(:all, :include => :comments, :order => "posts.id DESC")
- assert_equal @authorless, posts[0]
- assert_equal @thinking, posts[1]
- assert_equal @welcome, posts[2]
+ assert_equal posts(:authorless), posts[0]
+ assert_equal posts(:thinking), posts[1]
+ assert_equal posts(:welcome), posts[2]
end
def test_loading_with_multiple_associations
posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
assert_equal 2, posts.first.comments.size
assert_equal 2, posts.first.categories.size
- assert posts.first.comments.include?(@greetings)
+ assert posts.first.comments.include?(comments(:greetings))
end
def test_loading_from_an_association
- posts = @david.posts.find(:all, :include => :comments, :order => "posts.id")
+ posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
assert_equal 2, posts.first.comments.size
end
def test_loading_with_no_associations
- assert_nil Post.find(@authorless.id, :include => :author).author
+ assert_nil Post.find(posts(:authorless).id, :include => :author).author
end
def test_eager_association_loading_with_belongs_to
comments = Comment.find(:all, :include => :post)
- assert_equal @welcome.title, comments.first.post.title
- assert_equal @thinking.title, comments.last.post.title
+ assert_equal posts(:welcome).title, comments.first.post.title
+ assert_equal posts(:thinking).title, comments.last.post.title
end
def test_eager_association_loading_with_habtm
posts = Post.find(:all, :include => :categories, :order => "posts.id")
assert_equal 2, posts[0].categories.size
assert_equal 1, posts[1].categories.size
assert_equal 0, posts[2].categories.size
- assert posts[0].categories.include?(@technology)
- assert posts[1].categories.include?(@general)
+ assert posts[0].categories.include?(categories(:technology))
+ assert posts[1].categories.include?(categories(:general))
end
def test_eager_with_inheritance
posts = SpecialPost.find(:all, :include => [ :comments ])
end
+
+ def test_eager_with_has_one_dependent_does_not_destroy_dependent
+ assert_not_nil companies(:first_firm).account
+ f = Firm.find(:first, :include => :account,
+ :conditions => ["companies.name = ?", "37signals"])
+ assert_not_nil companies(:first_firm, :reload).account
+ end
end
Oops, something went wrong.

0 comments on commit 7f9ffb2

Please sign in to comment.