# Understanding â€œcreate\_associationâ€ and i nvalid objects

**URL:** <https://www.ruby-forum.com/t/understanding-a-oecreate-associationa-and-i-nvalid-objects/180679>\
**Category:** Rails\
**Created:** [December 25, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/understanding-a-oecreate-associationa-and-i-nvalid-objects/180679 "2009-12-25T17:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 25, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/understanding-a-oecreate-associationa-and-i-nvalid-objects/180679/1 "2009-12-25T17:16:43Z")

</div>

The documentation on the “create\_association” method of the “belongs\_to”  
association reads:

> create\_association(attributes = {})  
> Returns a new object of the associated type that has been instantiated with attributes, linked to this object through a foreign key, and that has already been saved (if it passed the validation).\*

My expectation from this documentation is that calling  
“object.create\_association” will return the newly created object if both  
objects save successfully and “false” if either object fails to save  
(due to failed validations, for example); or throw an error.

My experience, however, is that “create\_association” returns the newly  
created object regardless of whether it saved successfully or not.

How can I catch and handle a situation where “create\_association”  
instantiates and links a new object but is unable to save it?

Rails 2.3.5

* * *

\*[ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001835)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 25, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/understanding-a-oecreate-associationa-and-i-nvalid-objects/180679/2 "2009-12-25T17:39:36Z")

</div>

On Fri, Dec 25, 2009 at 12:02 AM, Evan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> and that has already been saved (if it passed the validation)

I think that “and that has already been saved (if it passed the  
validation)” means only that the new object will only be saved if it  
passed the validations, not that it won’t be instantiated in either  
case.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 12:28am UTC](https://www.ruby-forum.com/t/understanding-a-oecreate-associationa-and-i-nvalid-objects/180679/3 "2009-12-31T00:28:36Z")

</div>

> the new object will only be saved if it  
> passed the validations, not that it won’t be instantiated in either  
> case.

How can I catch and handle a situation where this occurs (the new  
object is instantiated, but not saved)?

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
