# Validation errors in has\_one/belongs\_to relationships

**URL:** <https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063>\
**Category:** Rails\
**Created:** [October 13, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063 "2006-10-13T22:27:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 13, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063/1 "2006-10-13T22:27:10Z")

</div>

I have two objects, QuoteInput and Insured.

QuoteInput has one Insured and Insured belongs to QuoteInput.

I don’t understand what I’m seeing.

I assign an Insured to a QuoteInput. The Insured object has invalid  
data.

I call save! on the quote input. QuoteInput has no validations.

I expect two things to happen - 1) an exception should be thrown,  
indicating that there was a problem saving the child object, and 2) an  
error message should be added to the quote\_input object’s error message  
array to indicate an error saving the child (Insured) object.

I know that the validation works on the insured object alone if I create  
one and attempt to save it with invalid data.

Why does qi.save! return true below? It’s like it didn’t try to save  
the insured object.

In script/console I see:

> > ins = Insured.new  
> > =\> #\<Insured:0x37345e0 @new\_record=true, @attributes={“city”=\>"",  
> > “address1”=\>""  
> > , “name”=\>"", “updated\_at”=\>nil, “address2”=\>nil, “zip\_code”=\>"",  
> > “dba\_name”=\>ni  
> > l, “quote\_input\_id”=\>nil, “created\_at”=\>nil, “state”=\>""}\>

> > ins.save!  
> > ActiveRecord::RecordInvalid: Validation failed: Name can’t be blank, Zip  
> > code co  
> > de is invalid  
> > from  
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_recor  
> > d/validations.rb:748:in `save!’  
> > from (irb):97  
> > from â?¥:0

> > qi = QuoteInput.new  
> > =\> #\<QuoteInput:0x36fb040 @new\_record=true, @program\_answers=[],  
> > @attributes={“p  
> > rogram\_id”=\>0, “updated\_at”=\>nil, “program\_quote\_input\_type”=\>"",  
> > “program\_quote  
> > \_input\_id”=\>0, “user\_id”=\>0, “created\_at”=\>nil}, @company\_answers=[]\>

> > qi.insured = ins  
> > =\> #\<Insured:0x37345e0 @new\_record=true,  
> > @errors=#\<ActiveRecord::Errors:0x372594  
> > 0 @errors={“name”=\>[“can’t be blank”], “zip\_code”=\>[“code is invalid”]},  
> > @base=#  
> > \<Insured:0x37345e0 …\>\>, @attributes={“city”=\>"", “address1”=\>"",  
> > “name”=\>"", "  
> > updated\_at"=\>nil, “address2”=\>nil, “zip\_code”=\>"", “dba\_name”=\>nil,  
> > “quote\_input  
> > \_id”=\>nil, “created\_at”=\>nil, “state”=\>""}\>

> > qi.save!  
> > =\> true

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 13, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063/2 "2006-10-13T22:54:39Z")

</div>

More succinctly stated, does the saving of an object (A) that :has\_one  
child object (B) cause an error to be attached to the object (A) if the  
child object (B) save fails?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 1, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063/3 "2008-02-01T15:44:00Z")

</div>

Hello,  
I’m wondering if you evere managed to resolve this issue? I’m having  
the same problem…  
Thanks,  
Eddie

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 13, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/validation-errors-in-has-one-belongs-to-relationships/72063/4 "2006-10-13T23:19:42Z")

</div>

I thought that perhaps using the build\_\<child\_object\> method that you  
get from declaring :has\_one \<other\_object\> might help. The console  
output is below.

What I get from this is that, given A and B where A :has\_one B:

1. A.save! causes a save attempt on B
2. If B’s save fails, then B gets errors put on its error array, A.save!  
returns true, and no exception is thrown.

This implies to me that if I want to be sure that A.save! successfully  
saved B, then I have to override A.save! to inspect B’s error array and  
then handle that.

Frankly, if I have to override save! in order to figure out whether the  
child objects actually saved, then what’s the point of save! calling  
save on the children for me? That’s only going to save me effort when  
I’m _absolutely sure_ (and when can you be absolutely sure) that the  
child objects will be saved successfully.

I will set up a simpler test case and reverify this result soon.

Thanks,  
Wes

==========================================

> > qi = QuoteInput.new  
> > =\> #\<QuoteInput:0x39c3fc0 @new\_record=true, @program\_answers=[],  
> > @attributes={“p  
> > rogram\_id”=\>0, “updated\_at”=\>nil, “program\_quote\_input\_type”=\>"",  
> > “program\_quote  
> > \_input\_id”=\>0, “user\_id”=\>0, “created\_at”=\>nil}, @company\_answers=[]\>

> > qi.build\_insured  
> > =\> #\<Insured:0x394e048 @new\_record=true, @attributes={“city”=\>"",  
> > “address1”=\>""  
> > , “name”=\>"", “updated\_at”=\>nil, “address2”=\>nil, “zip\_code”=\>"",  
> > “dba\_name”=\>ni  
> > l, “quote\_input\_id”=\>nil, “created\_at”=\>nil, “state”=\>""}\>

> > qi  
> > =\> #\<QuoteInput:0x39c3fc0 @new\_record=true, @program\_answers=[],  
> > @attributes={“p  
> > rogram\_id”=\>0, “updated\_at”=\>nil, “program\_quote\_input\_type”=\>"",  
> > “program\_quote  
> > \_input\_id”=\>0, “user\_id”=\>0, “created\_at”=\>nil},  
> > @insured=#\<Insured:0x394e048 @n  
> > ew\_record=true, @attributes={“city”=\>"", “address1”=\>"", “name”=\>"",  
> > “updated\_at  
> > “=\>nil, “address2”=\>nil, “zip\_code”=\>””, “dba\_name”=\>nil,  
> > “quote\_input\_id”=\>nil,  
> > “created\_at”=\>nil, “state”=\>""}\>, @company\_answers=[]\>

> > qi.save!  
> > =\> true

> > qi.errors  
> > =\> #\<ActiveRecord::Errors:0x3944430 @errors={},  
> > @base=#\<QuoteInput:0x39c3fc0 @ne  
> > w\_record=false, @program\_answers=[],  
> > @errors=#\<ActiveRecord::Errors:0x3944430 …  
> > .\>, @new\_record\_before\_save=true, @attributes={“program\_id”=\>0,  
> > “updated\_at”=\>Fr  
> > i Oct 13 16:12:40 Central Daylight Time 2006,  
> > “program\_quote\_input\_type”=\>"", “p  
> > rogram\_quote\_input\_id”=\>0, “id”=\>280, “user\_id”=\>0, “created\_at”=\>Fri  
> > Oct 13 16:  
> > 12:40 Central Daylight Time 2006}, @insured=#\<Insured:0x394e048  
> > @new\_record=true  
> > , @errors=#\<ActiveRecord::Errors:0x3932a00 @errors={“name”=\>[“can’t be  
> > blank”]},  
> > @base=#\<Insured:0x394e048 …\>\>, @attributes={“city”=\>"",  
> > “address1”=\>"", “name  
> > “=\>””, “updated\_at”=\>nil, “address2”=\>nil, “zip\_code”=\>"",  
> > “dba\_name”=\>nil, “quo  
> > te\_input\_id”=\>280, “created\_at”=\>nil, “state”=\>""}\>,  
> > @company\_answers=[]\>\>

> > qi.insured.errors  
> > =\> #\<ActiveRecord::Errors:0x3932a00 @errors={“name”=\>[“can’t be  
> > blank”]}, @base=  
> > #\<Insured:0x394e048 @new\_record=true,  
> > @errors=#\<ActiveRecord::Errors:0x3932a00 .  
> > …\>, @attributes={“city”=\>"", “address1”=\>"", “name”=\>"",  
> > “updated\_at”=\>nil, “ad  
> > dress2”=\>nil, “zip\_code”=\>"", “dba\_name”=\>nil, “quote\_input\_id”=\>280,  
> > “created\_a  
> > t”=\>nil, “state”=\>""}\>\>
