aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-11-27 13:05:19 -0800
committerJon Leighton <j@jonathanleighton.com>2011-11-27 13:05:19 -0800
commitb00cf122e2cb9bb326b2ec5ba4c35c1558362109 (patch)
tree80cf0a141eba7daff8d2d0950717a44197ef9500 /activerecord/test
parentaf3aacaa47922e1c587ce626bd45fd9f452fe03f (diff)
parent0f5104d7208b706e9a0353d3d4ccb20683e81bde (diff)
downloadrails-b00cf122e2cb9bb326b2ec5ba4c35c1558362109.tar.gz
rails-b00cf122e2cb9bb326b2ec5ba4c35c1558362109.tar.bz2
rails-b00cf122e2cb9bb326b2ec5ba4c35c1558362109.zip
Merge pull request #3748 from samsonasu/has_many_custom_pk_new_record
New records should load has_many relationships with custom primary keys
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb29
1 files changed, 28 insertions, 1 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index a60af7c046..88d7d47aea 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -8,6 +8,7 @@ require 'models/reply'
require 'models/category'
require 'models/post'
require 'models/author'
+require 'models/essay'
require 'models/comment'
require 'models/person'
require 'models/reader'
@@ -61,7 +62,7 @@ end
class HasManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :categories, :companies, :developers, :projects,
:developers_projects, :topics, :authors, :comments,
- :people, :posts, :readers, :taggings, :cars
+ :people, :posts, :readers, :taggings, :cars, :essays
def setup
Client.destroyed_client_ids.clear
@@ -1390,6 +1391,32 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
firm.clients.last
end
end
+
+ def test_custom_primary_key_on_new_record_should_fetch_with_query
+ author = Author.new(:name => "David")
+ assert !author.essays.loaded?
+
+ assert_queries 1 do
+ assert_equal 1, author.essays.size
+ end
+
+ assert_equal author.essays, Essay.find_all_by_writer_id("David")
+
+ end
+
+ def test_has_many_custom_primary_key
+ david = authors(:david)
+ assert_equal david.essays, Essay.find_all_by_writer_id("David")
+ end
+
+ def test_blank_custom_primary_key_on_new_record_should_not_run_queries
+ author = Author.new
+ assert !author.essays.loaded?
+
+ assert_queries 0 do
+ assert_equal 0, author.essays.size
+ end
+ end
def test_calling_first_or_last_with_find_options_on_loaded_association_should_fetch_with_query
firm = companies(:first_firm)