# Destroy association in memory

**URL:** <https://www.ruby-forum.com/t/destroy-association-in-memory/192021>\
**Category:** Rails\
**Created:** [July 22, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021 "2010-07-22T14:28:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anatoliy\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Anatoliy\_L](https://www.ruby-forum.com/u/Anatoliy_L)\
**Post date:** [July 22, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021/1 "2010-07-22T14:28:13Z")

</div>

Hi,  
Does Rails antipode of build method?  
Build make object in memory, and save it to database when parent  
object is saved.  
I need method to destroy object in memory and remove it from database  
when parent object is saved.  
mark\_for\_destruction is not the answer, because it not destroy object,  
only mark it.

Thanks,  
Anatoliy.

---

<div class="post-metadata">

**Author:** ![Anatoliy\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Anatoliy\_L](https://www.ruby-forum.com/u/Anatoliy_L)\
**Post date:** [July 22, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021/2 "2010-07-22T14:58:40Z")

</div>

Hi Anatoliy,

On Thu, Jul 22, 2010 at 6:42 AM, Anatoliy L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need method to destroy object in memory and remove it from database  
> when parent object is saved.

The notion of children that exiss prior to the parent’s creation is a  
bit odd, but assuming that you have some way to identify them other  
than session variables, you could use an after\_create method in your  
parent model to destroy the child objects.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![Anatoliy\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Anatoliy\_L](https://www.ruby-forum.com/u/Anatoliy_L)\
**Post date:** [July 22, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021/3 "2010-07-22T15:29:42Z")

</div>

Hi Bill,  
I was not clear,

Given situation when we have parent object task, and child object  
resource,  
task has many resources.  
When user edit task, change some fields, delete some resource  
association,  
add some resource association  
Notice than user not create resource or task, it create only association  
task ↔ resource  
Then user save task  
And task is not saved due to validation error  
Then changed fields is changed in memory, not database  
Then for added resource association builded in memory, not in database  
#the issue is here  
Then for deleted association to resource we have two choices:  
delete association to resource both in memory and database  
or  
mark it for destruction and have it both in memory and database

I need:  
destroy association to resource in memory, not in database, and destroy  
association in database only when task saved

Something like mark\_for\_destruction, but when you mark object it is  
still in  
association.

Thanks,  
Anatoliy

2010/7/22 Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Anatoliy\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Anatoliy\_L](https://www.ruby-forum.com/u/Anatoliy_L)\
**Post date:** [July 22, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021/4 "2010-07-22T16:57:32Z")

</div>

On 22 July 2010 14:28, Anatoliy L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Bill,  
> I was not clear,
> 
> Given situation when we have parent object task, and child object resource,  
> task has many resources.

Is that also resource belongs\_to task?

> When user edit task, change some fields, delete some resource association,  
> add some resource association  
> Notice than user not create resource or task, it create only association  
> task ↔ resource

So you are just setting the id field in an existing resource in the db?

> Then user save task  
> And task is not saved due to validation error  
> Then changed fields is changed in memory, not database  
> Then for added resource association builded in memory, not in database

What do you mean the association is built in memory? Also nothing is  
saved in memory from one request cycle to the next, so I do not  
understand what you are trying to say.

Colin

---

<div class="post-metadata">

**Author:** ![Anatoliy\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Anatoliy\_L](https://www.ruby-forum.com/u/Anatoliy_L)\
**Post date:** [July 26, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/destroy-association-in-memory/192021/5 "2010-07-26T15:44:13Z")

</div>

Hi Everyone,  
Thanks for responses,  
I found what I need:  
[#4521 HABTM: ability to link and unlink records with :autosave =\> true - Ruby on Rails - rails](http://rails.lighthouseapp.com/projects/8994/tickets/4521-habtm-ability-to-link-and-unlink-records-with-autosave-true)[https://rails.lighthouseapp.com/projects/8994/tickets/4521-habtm-ability-to-link-and-unlink-records-with-autosave-true](https://rails.lighthouseapp.com/projects/8994/tickets/4521-habtm-ability-to-link-and-unlink-records-with-autosave-true)

Thanks,  
Anatoliy.

2010/7/22 Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
