On Sat, Dec 25, 2021 at 10:53 PM jubishop <[email protected]> wrote:
> I would like to have an item automatically destroyed when a necessary
> association has been removed via a call to a `remove_association` method.
> I have achieved this right now with an extra bit of markup that looks like
> this:
>
> ```ruby
> class Group < Sequel::Model
> one_to_many :members, remover: ->(member) { member.destroy }`
> end
> ```
>
> this seems to work, and any `Member` instance is destroyed when I call
> `remove_member(member)` on a `Group` instance. but I'm curious if this is
> an appropriate and best way of doing this. It'd be great if there was an
> option/plugin that would do this automatically when the db schema migration
> specifies something like:
>
> ```ruby
> create_table(:members) {
> foreign_key :group_id, :groups, null: false, on_delete: :cascade
> }
> ```
>
> thanks for any help and apologies if I've missed this in the documentation
> or am conceptually being stupid :)
>
You could probably simplify it slightly:
remover: :destroy.to_proc
Considering the simplicity of that approach, I don't think it makes sense
to introduce an additional option/plugin for it.
The schema migration syntax (which does ON DELETE CASCADE), requires
support at the database server. If you want something similar for what you
are doing (delete at remove), you are looking at writing a database
trigger. A database trigger may be a better way to implement what you want,
considering that would also handle remove_all_members (which the :remover
option won't handle). However, it's more complex to implement, especially
if you are not experienced with writing database triggers.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/CADGZSSdf_aohc_5s9vF8iEpZfas2VAKQY_uZwE6Zx%2BLtRvFSdQ%40mail.gmail.com.