# Has\_one and has\_many creation inconsistencies

**URL:** <https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395>\
**Category:** Rails\
**Created:** [July 7, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395 "2008-07-07T15:29:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 7, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/1 "2008-07-07T15:29:20Z")

</div>

So, imagine the following:

class User \< ActiveRecord::Base  
has\_many :emails  
has\_one :address  
end

class Email \< ActiveRecord::Base  
belongs\_to :user  
end

class Address \< ActiveRecord::Base  
belongs\_to :user  
end

Now I am wondering what the correct Ruby-way to create a user object is  
in the controller when through a form, a new user, a couple of email  
addresses and a street address is posted.

The obvious @user = User.new(params[:user]) was my first attempt. The  
problem here is that @user.emails.build and @user.address.build calls  
are fine, until we try to save the @user object. It then gives an error  
that it can not save the emails and address, because it doesn’t know  
what user\_id to write in its foreign keys.

The only way I could devise was an intricate scheme where I would first  
try to save the emails and address, and then the user object. Whenever  
something would fail along the way it would rollback any previous  
changes and destroy the correct object. This results in a very complex  
non-ruby if/then stack which I really hate.

How am I supposed to save an object spanning multiple models at once and  
consistent?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 7, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/2 "2008-07-07T15:46:44Z")

</div>

There is an explanation of this in the Rails API documentation here:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

Scroll down to the section: Unsaved objects and associations

On Jul 7, 9:29 am, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 7, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/3 "2008-07-07T16:08:27Z")

</div>

After looking through that documentation and your scenario, I suppose  
one possibility is to perform your saves in a database transaction:

transaction do

# save the association objects

# save the user object

end

So if any failure occurs during save the whole transaction gets rolled  
back. I’m not sure if this is the best way to do this though.

It does seem like it would be nice if Rails would automatically create  
a transaction if an object has unsaved associations. Then perform  
saves on the associations then save the parent object. But, I’m sure  
there’s probably a good reason that it doesn’t.

That being said, I’m more used to a system that maintains an in-memory  
object graph that takes care of all those details for you. However,  
that opens up a whole new level of complexity, and a new set of  
potential gotchas to have to deal with.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 7, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/4 "2008-07-07T16:01:25Z")

</div>

> So, imagine the following:

class Recipe  
has\_many :ingredients  
end

class Ingredient  
belongs\_to :recipe  
end

this is my console output

> > r = Recipe.new(:name =\> ‘spaghetti carbonara’)  
> > =\> #\<Recipe id: nil, name: “spaghetti carbonara”, description: nil,  
> > created\_at: nil, updated\_at: nil\>  
> > r.ingredients.build( :name =\> ‘pasta’ )  
> > =\> #\<Ingredient id: nil, name: “pasta”, recipe\_id: nil, created\_at:  
> > nil, updated\_at: nil\>  
> > r.ingredients  
> > =\> [#\<Ingredient id: nil, name: “pasta”, recipe\_id: nil, created\_at:  
> > nil, updated\_at: nil\>]  
> > r.ingredients.first.recipe\_id  
> > =\> nil  
> > r.save  
> > =\> true  
> > r.ingredients.first.recipe\_id  
> > =\> 4

Im on rails 2.1 out of the box.

On Jul 7, 2:29 pm, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 8, 2008, 1:59pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/5 "2008-07-08T13:59:37Z")

</div>

> Well then…
> 
> Disregard my last post. Apparently, at least with Rails 2.1, it does  
> seems to take care of the associations when saving the parent.

Rails has always done wrapped new parent/child commits in a  
transaction; that’s nothing new to 2.1.

I suspect that Chris’ issue has to do with validations. My guess is  
that what he’s omitted from his class declarations in the initial post  
is that he’s validating for the presence/numericality of user\_id but  
it’s not yet known. If my guess is correct then it’s a pretty simple  
refinement of the validation call.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 7, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/has-one-and-has-many-creation-inconsistencies/142395/6 "2008-07-07T16:14:19Z")

</div>

Well then…

Disregard my last post. Apparently, at least with Rails 2.1, it does  
seems to take care of the associations when saving the parent.
