# Replacement for AR's dependent: destroy

**URL:** <https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462>\
**Category:** Issues\
**Created:** [May 11, 2021, 3:53pm UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462 "2021-05-11T15:53:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ineu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ineu/32/235_2.png) [@ineu](https://discourse.rom-rb.org/u/ineu)\
**Post date:** [May 11, 2021, 3:53pm UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/1 "2021-05-11T15:53:34Z")

</div>

Hello.  
I have started playing with ROM in a simple Rails project, gradually reimplementing functionality to study ROM. I have faced few issues that’s not relevant to this topic, I might open other tickets for them later.  
The thing I haven’t found a good solution for is the ActiveRecord’s `dependent: destroy` (or `delete_all`, or `nullify`). If I were using raw Sequel with Sequel models, there’s a plugin that does exactly this: [Sequel::Plugins::AssociationDependencies](https://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/AssociationDependencies.html). And, obviously, it’s built into AR.  
So I wonder what are the best practices to achieve similar behavior in ROM? For now I’m manually removing dependent records via their relation, but such code should definitely be hidden somehow (a custom Command?)  
Here’s the quick controller action:

```auto
  def destroy
    id = Integer(params[:id])
    ROM.env.relations[:line_items].where(invoice_id: id).delete
    repo.delete(id)

    redirect_to invoices_url(filter: 'new'), flash: { notice: "Инвойс удалён" }
  end

```

Thanks.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [May 12, 2021, 8:15am UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/2 "2021-05-12T08:15:24Z")

</div>

It is recommended to use foreign keys with proper on\_delete triggers. This is a much more robust solution.

---

<div class="post-metadata">

**Author:** ![ineu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ineu/32/235_2.png) [@ineu](https://discourse.rom-rb.org/u/ineu)\
**Post date:** [May 12, 2021, 9:04am UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/3 "2021-05-12T09:04:48Z")

</div>

Do you mean the ON DELETE CASCADE on the database level?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [May 13, 2021, 8:08am UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/4 "2021-05-13T08:08:00Z")

</div>

Yes exactly 🙂 And here goes more words because Discourse needs at least 20 chars 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![ineu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ineu/32/235_2.png) [@ineu](https://discourse.rom-rb.org/u/ineu)\
**Post date:** [May 13, 2021, 8:21am UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/5 "2021-05-13T08:21:43Z")

</div>

That’s indeed a better way to go, thank you.  
I’d like to add some documentation on this case plus some issues that I found while investigating the docs for the first time. Are you guys willing to accept such type of a PR?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [May 15, 2021, 8:51am UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/6 "2021-05-15T08:51:56Z")

</div>

> [@ineu](#):
>
> I’d like to add some documentation on this case plus some issues that I found while investigating the docs for the first time. Are you guys willing to accept such type of a PR?

That would be lovely! Yes help with docs is always welcome.

---

<div class="post-metadata">

**Author:** ![kenberland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/kenberland/32/255_2.png) [@kenberland](https://discourse.rom-rb.org/u/kenberland)\
**Post date:** [January 31, 2022, 4:04pm UTC](https://discourse.rom-rb.org/t/replacement-for-ars-dependent-destroy/462/7 "2022-01-31T16:04:14Z")

</div>

Did these changes ever make it into the project? I’m having the same issue.
