Skip to content
This repository
Browse code

has_many :through create should not raise validation errors

[#2934 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 33c054d7e04672201e120a0f8da38166650f3a94 1 parent cc3183d
Dan Croak authored August 09, 2009 jeremy committed August 09, 2009
12  activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -17,7 +17,17 @@ def create!(attrs = nil)
17 17
 
18 18
       def create(attrs = nil)
19 19
         transaction do
20  
-          self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
  20
+          object = if attrs
  21
+            @reflection.klass.send(:with_scope, :create => attrs) {
  22
+              @reflection.create_association
  23
+            }
  24
+          else
  25
+            @reflection.create_association
  26
+          end
  27
+          raise_on_type_mismatch(object)
  28
+          add_record_to_target_with_callbacks(object) do |r|
  29
+            insert_record(object, false)
  30
+          end
21 31
           object
22 32
         end
23 33
       end
30  activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -11,9 +11,13 @@
11 11
 require 'models/owner'
12 12
 require 'models/pet'
13 13
 require 'models/toy'
  14
+require 'models/contract'
  15
+require 'models/company'
  16
+require 'models/developer'
14 17
 
15 18
 class HasManyThroughAssociationsTest < ActiveRecord::TestCase
16  
-  fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys
  19
+  fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys,
  20
+           :companies
17 21
 
18 22
   def test_associate_existing
19 23
     assert_queries(2) { posts(:thinking);people(:david) }
@@ -157,6 +161,30 @@ def test_associate_with_create_exclamation_and_no_options
157 161
     assert_equal peeps + 1, posts(:thinking).people.count
158 162
   end
159 163
 
  164
+  def test_associate_with_create_and_invalid_options
  165
+    peeps = companies(:first_firm).developers.count
  166
+    assert_nothing_raised { companies(:first_firm).developers.create(:name => '0') }
  167
+    assert_equal peeps, companies(:first_firm).developers.count
  168
+  end
  169
+
  170
+  def test_associate_with_create_and_valid_options
  171
+    peeps = companies(:first_firm).developers.count
  172
+    assert_nothing_raised { companies(:first_firm).developers.create(:name => 'developer') }
  173
+    assert_equal peeps + 1, companies(:first_firm).developers.count
  174
+  end
  175
+
  176
+  def test_associate_with_create_bang_and_invalid_options
  177
+    peeps = companies(:first_firm).developers.count
  178
+    assert_raises(ActiveRecord::RecordInvalid) { companies(:first_firm).developers.create!(:name => '0') }
  179
+    assert_equal peeps, companies(:first_firm).developers.count
  180
+  end
  181
+
  182
+  def test_associate_with_create_bang_and_valid_options
  183
+    peeps = companies(:first_firm).developers.count
  184
+    assert_nothing_raised { companies(:first_firm).developers.create!(:name => 'developer') }
  185
+    assert_equal peeps + 1, companies(:first_firm).developers.count
  186
+  end
  187
+
160 188
   def test_clear_associations
161 189
     assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
162 190
     
4  activerecord/test/cases/reflection_test.rb
@@ -170,8 +170,8 @@ def test_association_reflection_in_modules
170 170
 
171 171
   def test_reflection_of_all_associations
172 172
     # FIXME these assertions bust a lot
173  
-    assert_equal 28, Firm.reflect_on_all_associations.size
174  
-    assert_equal 21, Firm.reflect_on_all_associations(:has_many).size
  173
+    assert_equal 30, Firm.reflect_on_all_associations.size
  174
+    assert_equal 23, Firm.reflect_on_all_associations(:has_many).size
175 175
     assert_equal 7, Firm.reflect_on_all_associations(:has_one).size
176 176
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
177 177
   end
2  activerecord/test/models/company.rb
@@ -9,6 +9,8 @@ class Company < AbstractCompany
9 9
   validates_presence_of :name
10 10
 
11 11
   has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
  12
+  has_many :contracts
  13
+  has_many :developers, :through => :contracts
12 14
 
13 15
   def arbitrary_method
14 16
     "I am Jack's profound disappointment"
5  activerecord/test/models/contract.rb
... ...
@@ -0,0 +1,5 @@
  1
+class Contract < ActiveRecord::Base
  2
+  belongs_to :company
  3
+  belongs_to :developer
  4
+end
  5
+
4  activerecord/test/schema/schema.rb
@@ -125,6 +125,10 @@ def create_table(*args, &block)
125 125
     t.integer :extendedWarranty, :null => false
126 126
   end
127 127
 
  128
+  create_table :contracts, :force => true do |t|
  129
+    t.integer :developer_id
  130
+    t.integer :company_id
  131
+  end
128 132
 
129 133
   create_table :customers, :force => true do |t|
130 134
     t.string  :name

0 notes on commit 33c054d

Please sign in to comment.
Something went wrong with that request. Please try again.