# Saving associated models...is there any order to it?

**URL:** <https://www.ruby-forum.com/t/saving-associated-models-is-there-any-order-to-it/143232>\
**Category:** Rails\
**Created:** [July 16, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/saving-associated-models-is-there-any-order-to-it/143232 "2008-07-16T16:42:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Frith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@Ryan\_Frith](https://www.ruby-forum.com/u/Ryan_Frith)\
**Post date:** [July 16, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/saving-associated-models-is-there-any-order-to-it/143232/1 "2008-07-16T16:42:25Z")

</div>

Hi,

If I have 2 models defined as such:

class ClassA \< ActiveRecord::Base  
before\_create “class\_b.foo”  
has\_one :class\_b  
def foobar  
puts “foobar”  
end  
end

ClassB \< ActiveRecord::Base  
after\_create :bar  
belongs\_to :class\_a  
def foo  
puts “foo”  
end  
def bar  
class\_a.foobar  
end  
end

And somewhere I do this:

a = ClassA.new  
b = ClassB.new  
a.class\_b = b  
a.save

Which object will get created first, and in turn, have it’s lifecycle  
method called first? In other words, will ClassA definitely get  
created before ClassB, or vise versa? Is there any guarantee to the  
order of saving objects in the graph?

Thanks in advance,

Ryan

---

<div class="post-metadata">

**Author:** ![Ryan\_Frith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@Ryan\_Frith](https://www.ruby-forum.com/u/Ryan_Frith)\
**Post date:** [July 16, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/saving-associated-models-is-there-any-order-to-it/143232/2 "2008-07-16T22:52:40Z")

</div>

Anybody?
