From 627d51800f256af280354847a7a3c5bd6309a589 Mon Sep 17 00:00:00 2001 From: railsbob Date: Sat, 8 Aug 2009 21:01:28 +0100 Subject: [PATCH] Fixed association_collection#create exceptions --- .../associations/association_collection.rb | 2 +- .../has_many_through_associations_test.rb | 37 +++++++++++++++++++- activerecord/test/cases/reflection_test.rb | 4 +- activerecord/test/models/company.rb | 2 + activerecord/test/models/contract.rb | 4 ++ activerecord/test/schema/schema.rb | 4 ++ 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 activerecord/test/models/contract.rb diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index e67ccfb..7f29e9c 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -116,7 +116,7 @@ module ActiveRecord flatten_deeper(records).each do |record| raise_on_type_mismatch(record) add_record_to_target_with_callbacks(record) do |r| - result &&= insert_record(record) unless @owner.new_record? + result &&= insert_record(record, false) unless @owner.new_record? end end end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 8529ff0..7c076df 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -11,9 +11,12 @@ require 'models/author' require 'models/owner' require 'models/pet' require 'models/toy' +require 'models/contract' +require 'models/company' +require 'models/developer' class HasManyThroughAssociationsTest < ActiveRecord::TestCase - fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references + fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references, :companies def test_associate_existing assert_queries(2) { posts(:thinking);people(:david) } @@ -169,6 +172,38 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal peeps + 1, posts(:thinking).people.count end + def test_associate_with_create_and_invalid_options + peeps = companies(:first_firm).developers.count + assert_nothing_raised do + companies(:first_firm).developers.create(:name => '0') + end + assert_equal peeps, companies(:first_firm).developers.count + end + + def test_associate_with_create_and_valid_options + peeps = companies(:first_firm).developers.count + assert_nothing_raised do + companies(:first_firm).developers.create(:name => 'developer') + end + assert_equal peeps + 1, companies(:first_firm).developers.count + end + + def test_associate_with_create_bang_and_invalid_options + peeps = companies(:first_firm).developers.count + assert_raise(ActiveRecord::RecordInvalid) do + companies(:first_firm).developers.create!(:name => '0') + end + assert_equal peeps, companies(:first_firm).developers.count + end + + def test_associate_with_create_bang_and_valid_options + peeps = companies(:first_firm).developers.count + assert_nothing_raised(ActiveRecord::RecordInvalid) do + companies(:first_firm).developers.create!(:name => 'developer') + end + assert_equal peeps + 1, companies(:first_firm).developers.count + end + def test_clear_associations assert_queries(2) { posts(:welcome);posts(:welcome).people(true) } diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 4083b99..a164f5e 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -176,8 +176,8 @@ class ReflectionTest < ActiveRecord::TestCase def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 29, Firm.reflect_on_all_associations.size - assert_equal 22, Firm.reflect_on_all_associations(:has_many).size + assert_equal 31, Firm.reflect_on_all_associations.size + assert_equal 24, Firm.reflect_on_all_associations(:has_many).size assert_equal 7, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 1c05e52..ab09f88 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -9,6 +9,8 @@ class Company < AbstractCompany validates_presence_of :name has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account" + has_many :contracts + has_many :developers, :through => :contracts def arbitrary_method "I am Jack's profound disappointment" diff --git a/activerecord/test/models/contract.rb b/activerecord/test/models/contract.rb new file mode 100644 index 0000000..606c99c --- /dev/null +++ b/activerecord/test/models/contract.rb @@ -0,0 +1,4 @@ +class Contract < ActiveRecord::Base + belongs_to :company + belongs_to :developer +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 5f60d5e..9ab4cf6 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -131,6 +131,10 @@ ActiveRecord::Schema.define do t.integer :extendedWarranty, :null => false end + create_table :contracts, :force => true do |t| + t.integer :developer_id + t.integer :company_id + end create_table :customers, :force => true do |t| t.string :name -- 1.6.0.2