# Ensuring both objects in a one-to-many association are not saved without the presence of the other

**URL:** <https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306>\
**Category:** Rails\
**Created:** [October 20, 2008, 5:36am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306 "2008-10-20T05:36:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 5:36am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/1 "2008-10-20T05:36:59Z")

</div>

Hello.

I have an object which is related to a second object through has\_many.  
The second object (obviously) is related to the first through  
belongs\_to. I want to ensure that neither object is saved to the  
database without the presence of the other. How can I do this?

Thanks.

Evan

---

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 6:45am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/2 "2008-10-20T06:45:59Z")

</div>

Evan wrote:

> Hello.
> 
> I have an object which is related to a second object through has\_many.  
> The second object (obviously) is related to the first through  
> belongs\_to. I want to ensure that neither object is saved to the  
> database without the presence of the other. How can I do this?
> 
> Thanks.
> 
> Evan

Use transaction

---

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 7:01am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/3 "2008-10-20T07:01:45Z")

</div>

Even with transactions can I save the object containing the foreign  
key without saving the primary object first? In other words, can I  
save the object that requires the foreign key to be present if I don’t  
have the foreign key because the primary object has not yet been  
saved, thus generating an id for it?

---

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 7:45am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/4 "2008-10-20T07:45:36Z")

</div>

I guess the question I was really getting at is how can I validate the  
presence and association of both objects before they are saved? I feel  
more confident when there is code in the model itself to ensure the  
integrity of the data rather than depending on each programmer to  
place every save in a transaction block.

---

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 7:03am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/5 "2008-10-20T07:03:31Z")

</div>

Oh wait. I think I just got it. Start a transaction, save the primary  
object thus getting the id, save the secondary object, end  
transaction. Right?

---

<div class="post-metadata">

**Author:** ![evan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/54ee81/32.png) [@evan](https://www.ruby-forum.com/u/evan)\
**Post date:** [October 20, 2008, 11:45am UTC](https://www.ruby-forum.com/t/ensuring-both-objects-in-a-one-to-many-association-are-not-saved-without-the-presence-of-the-other/151306/6 "2008-10-20T11:45:32Z")

</div>

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001343](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001343)
