If you alias a method that is not directly defined in the current class, then alias
looks for the method in the nearest ancestor of the class in which it was executed.
When you include Trashable::InstanceMethods
into one of your models, it gets inserted at the front of that model's ancestor chain. Hence, calling destroy!
in that model triggers the destroy
method on Trashable::InstanceMethods
.
If you move def destroy
from InstanceMethods
to base.class_eval
, then it would be defined in the including model directly, and the nearest ancestor of that model that contains 'destroy' would be the relevant module in ActiveRecord. Therefore calling destroy!
would trigger an SQL DELETE
as expected.
See class.ancestors
to further explore this behavior.