# Advice on managing object graph in memory

**URL:** <https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896>\
**Category:** Rails\
**Created:** [February 2, 2008, 9:46am UTC](https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896 "2008-02-02T09:46:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [February 2, 2008, 9:46am UTC](https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896/1 "2008-02-02T09:46:34Z")

</div>

I’ve experienced several inconsistencies when I traverse related  
ActiveRecord objects before they are saved into database. For example:  
c = parent.children.build # =\> #Child:0x222222  
parent.children # =\> [#Child:0x222222]  
c.parent.children # =\> [] … it should return the  
same  
as parent.children

My question is this: is it generally a bad idea to interact with object  
graph that’s unsaved? While it’s common practice to build an object in  
memory, bounce it back and fourth between the view and the controller  
all  
without saving it, I find that kind of paradigm hard to achieve when I  
need  
to build multiple objects that are related to each other. It seems to  
only  
work well for a single object, in which case the only inconsistency is  
that  
it doesn’t have an ID yet.

Thoughts?  
Eric

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [February 2, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896/2 "2008-02-02T17:47:40Z")

</div>

Eric L. wrote:

> memory, bounce it back and fourth between the view and the controller  
> all without saving it, I find that kind of paradigm hard to achieve when  
> I need to build multiple objects that are related to each other. It  
> seems to only work well for a single object, in which case the only  
> inconsistency is that it doesn’t have an ID yet.
> 
> Thoughts?

You can manually stitch things together, such as back links for  
belongs\_to associations, and by adding c.parent = parent after  
the build in your example. Build should probably do this  
automatically.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [February 2, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896/3 "2008-02-02T23:58:22Z")

</div>

Hmm… that seems kind of inconvenient, but I’ll give it a try, thanks.

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [February 3, 2008, 3:10am UTC](https://www.ruby-forum.com/t/advice-on-managing-object-graph-in-memory/127896/4 "2008-02-03T03:10:11Z")

</div>

By the way, is the inability to back reference from child  
(c.parent.children  
) a generally known behavior, or is it something that should be fixed in  
the  
framework?
