[Feature idea] Allow custom dependent callbacks on has_many Associations

I was thinking of implementing a way to use custom method names for dependent has_many Associations. This way I wouldn’t have to use workarounds like rewriting destroy methods, or stuffing code handling other models into methods for before_destroy callbacks.

class Thing < ApplicationRecord

belongs_to :owner, class_name: ‘User’

def reset_owner

set owner to another user based on settings

end

end

class User < ApplicationRecord

has_many :things, dependent: :reset_owner

end

``

I don’t think other kinds of Associations would need to support this.

What do you think?

I love this, we have a tendency to soft delete records and so we want a custom callback to cascade the soft delete

has_many :things, dependent: :soft_delete

and then that could either call a method called soft_delete on each record or perhaps would call a custom class that you can perform logic in.

1 Like