Replacement for AR's dependent: destroy

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. 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:

  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.

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

1 Like

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

Yes exactly :slightly_smiling_face: And here goes more words because Discourse needs at least 20 chars :man_facepalming:

1 Like

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?

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

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