# Clear persistent model associations for objects in session

**URL:** <https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966>\
**Category:** Rails\
**Created:** [February 3, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966 "2008-02-03T14:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966/1 "2008-02-03T14:02:07Z")

</div>

I have an order form that is comprised of a lot of :has\_many and  
:has\_one associations. I want to load the object into the session, with  
all associations and edit it.

I want the whole submission process to be done atomically for objects at  
once. So if I add or edit items on the order, I don’t want to be saved  
to the database until the user clicks submit.

The problem is that I save the order in the session, but the  
associations get refreshed every time the page reloads. I looked at the  
code in ction\_controller/session\_management.rb and  
clear\_persistent\_model\_associations() takes care of removing  
associations.

Any suggestions on how to handle this?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966/2 "2008-02-03T15:01:52Z")

</div>

Constantin G. wrote:

> code in ction\_controller/session\_management.rb and  
> clear\_persistent\_model\_associations() takes care of removing  
> associations.
> 
> Any suggestions on how to handle this?

You can serialize and deserialize the whole structure with  
to\_json and from\_json, specifying the includes.

But because cookie sessions are limited to 4KB, you should  
probably store associated ids instead. e.g. create a model  
attribute that on read converts a has\_many to an array of ids,  
and on write populates the has\_many from an array. The  
association is re-loaded, but you re-create the right set of  
associates.

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

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966/3 "2008-02-03T20:28:28Z")

</div>

Mark Reginald J. wrote:

> Constantin G. wrote:
> 
> > code in ction\_controller/session\_management.rb and  
> > clear\_persistent\_model\_associations() takes care of removing  
> > associations.
> > 
> > Any suggestions on how to handle this?
> 
> You can serialize and deserialize the whole structure with  
> to\_json and from\_json, specifying the includes.

Marshal dump() and load() saves associated objects, too. Is there some  
specific reason why I should use json?

Maybe I need to explain what I want to achieve. I have an order with a  
multitude of associated associations which, to further complicate  
things, have more associations, like extra charges.

Something like:  
order has many containers  
container has many extra charges

The user edits the order, adds or deletes containers, adds or deletes  
extra charges to the containers. The problem is that I want all the  
changes to be saved the the user presses the “Save order” button, not  
when he adds or edits each container or extra charge. I wanted to save  
the whole order and associations in the session until it’s ready to save  
in one atomic operation, but it seems there’s no easy way to do this  
with the way Rails and ActiveRecord works.

> > Order.find(20).containers.size  
> > =\> 1
> 
> >

?\> o = Order.find(20)  
=\> #\<ImportOrder:0x46b3934 @attributes=…

> > o.containers.size  
> > =\> 1

> > o.containers = Hash.new #This will delete all containers in the database, not just in the memory.  
> > =\> {}

> Order.find(20).containers.size  
> =\> 0

So when I work with activerecord objects, they might get commited to the  
database when I don’t really want that.

> But because cookie sessions are limited to 4KB, you should  
> probably store associated ids instead. e.g. create a model  
> attribute that on read converts a has\_many to an array of ids,  
> and on write populates the has\_many from an array. The  
> association is re-loaded, but you re-create the right set of  
> associates.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [June 10, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966/4 "2008-06-10T21:18:32Z")

</div>

This seems to address the same problem that i’m trying to find a  
suitable workaround for.

I’ve tried your solution (redefining the clear\_association\_cache method  
on the parent object) and on first try it looks to be achieving the  
desired effect of maintaining the child data in the session.

I was just wondering if this solution ended up working for you  
long-term, or if any undesirable side effects appeared?

Thanks very much for your time,

jesse

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/clear-persistent-model-associations-for-objects-in-session/127966/5 "2008-02-03T22:32:09Z")

</div>

Constantin G. wrote:

> Maybe I need to explain what I want to achieve. I have an order with a  
> multitude of associated associations which, to further complicate  
> things, have more associations, like extra charges.
> 
> Something like:  
> order has many containers  
> container has many extra charges
> 
> The user edits the order, adds or deletes containers, adds or deletes  
> extra charges to the containers. The problem is that I want all the  
> changes to be saved the the user presses the “Save order” button, not  
> when he adds or edits each container or extra charge. I wanted to save  
> the whole order and associations in the session until it’s ready to save  
> in one atomic operation

I found a way to keep all my order objects into the session until they  
are ready to be saved: redefine clear\_association\_cache for class order  
and it’s subclasses.

def clear\_association\_cache  
return if self.class.to\_s =~ /Order$/  
super  
end
