@@ -26,7 +26,7 @@ def create
26
26
errors : {
27
27
employee : employee . errors ,
28
28
positions : employee . positions . map ( &:errors ) ,
29
- departments : employee . positions . map ( &:department ) . map ( &:errors )
29
+ departments : employee . positions . map ( &:department ) . compact . map ( &:errors )
30
30
}
31
31
}
32
32
end
@@ -509,6 +509,52 @@ def do_put(id)
509
509
expect ( positions [ 1 ] . title ) . to eq ( 'manager' )
510
510
expect ( department . name ) . to eq ( 'safety' )
511
511
end
512
+
513
+ context 'when a has_many relationship has validation error' do
514
+ around do |e |
515
+ begin
516
+ Position . validates :title , presence : true
517
+ e . run
518
+ ensure
519
+ Position . clear_validators!
520
+ end
521
+ end
522
+
523
+ before do
524
+ payload [ :included ] [ 0 ] [ :attributes ] . delete ( :title )
525
+ end
526
+
527
+ it 'rolls back the entire transaction' do
528
+ expect {
529
+ do_post
530
+ } . to_not change { Employee . count +Position . count +Department . count }
531
+ expect ( json [ 'errors' ] [ 'positions' ] )
532
+ . to eq ( [ { 'title' => [ "can't be blank" ] } , { } ] )
533
+ end
534
+ end
535
+
536
+ context 'when a belongs_to relationship has a validation error' do
537
+ around do |e |
538
+ begin
539
+ Department . validates :name , presence : true
540
+ e . run
541
+ ensure
542
+ Department . clear_validators!
543
+ end
544
+ end
545
+
546
+ before do
547
+ payload [ :included ] [ 1 ] [ :attributes ] . delete ( :name )
548
+ end
549
+
550
+ it 'rolls back the entire transaction' do
551
+ expect {
552
+ do_post
553
+ } . to_not change { Employee . count +Position . count +Department . count }
554
+ expect ( json [ 'errors' ] [ 'departments' ] )
555
+ . to eq ( [ { 'name' => [ "can't be blank" ] } ] )
556
+ end
557
+ end
512
558
513
559
context 'when associating to an existing record' do
514
560
let! ( :classification ) { Classification . create! ( description : 'senior' ) }
0 commit comments