# Marshaling ActiveRecord objects: how to unload associations?

**URL:** <https://www.ruby-forum.com/t/marshaling-activerecord-objects-how-to-unload-associations/129953>\
**Category:** Rails\
**Created:** [February 22, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/marshaling-activerecord-objects-how-to-unload-associations/129953 "2008-02-22T23:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [February 22, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/marshaling-activerecord-objects-how-to-unload-associations/129953/1 "2008-02-22T23:51:57Z")

</div>

I’m marshaling ActiveRecord objects, and I want them to be as small as  
possible. How can I unload everything that can be reloaded from the  
database?

If they’re marshaled before any of the associations are referenced  
then the referenced objects aren’t dumped. But if the association has  
been loaded, how can I unload it? AssociationProxy.reset doesn’t seem  
to do it. It seems that after the AssociationProxy has been touched at  
all then dumping the object causes the AssociationProxy to load  
everything.

Thanks!

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [March 4, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/marshaling-activerecord-objects-how-to-unload-associations/129953/2 "2008-03-04T20:49:48Z")

</div>

Paul D. wrote:

> I’m marshaling ActiveRecord objects, and I want them to be as small as  
> possible. How can I unload everything that can be reloaded from the  
> database?
> 
> If they’re marshaled before any of the associations are referenced  
> then the referenced objects aren’t dumped. But if the association has  
> been loaded, how can I unload it? AssociationProxy.reset doesn’t seem  
> to do it. It seems that after the AssociationProxy has been touched at  
> all then dumping the object causes the AssociationProxy to load  
> everything.
> 
> Thanks!

Try calling these methods on your activerecord:

```
    clear_aggregation_cache
    clear_association_cache

```

They are not private/protected so you can call them from outside an  
object as well.

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [March 4, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/marshaling-activerecord-objects-how-to-unload-associations/129953/3 "2008-03-04T20:51:19Z")

</div>

Try calling these methods on your activerecord:

```
    clear_aggregation_cache
    clear_association_cache

```

They are not private/protected so you can call them from outside an  
object as well.
