everett1992
everett1992

Reputation: 2661

skip callback in inherited model

Is there a method like ActionController::Base#skip_before_filter for ActiveRecord callbacks like after_create? I have a few classes that behave similarly, and to reduce code repetition I created an abstract class. The abstraction contains relation information, validation and an after_create callback that creates a new message. Message implements this abstractions, so saving the message causes an stack overflow.

Can I remove a callback from a model?

class Parent < ActiveRecord::Base
    self.abstract_class = true
    after_create :notify

    def notify
        Message.create
    end
end

class Message < Parent
    # skip after_create :notify
end

class Child < Parent
end

Upvotes: 4

Views: 1918

Answers (1)

Jun Zhou
Jun Zhou

Reputation: 3080

class Message < Parent
  skip_callback :create, :after, :notify
end

Check Rails Documentation for details

Upvotes: 6

Related Questions