1

I have a few classes deriving from A. A does some validation In the specific case of class B that inherits from A, I'd like to skip the validation.

I'm using active interaction btw

class A < ActiveInteraction::Base
  string :s

  validate :valid
        
  private

  def vaild
    #raise something unless s equals "banana"
  end
end


class B < A 
  #do something here to skip A's validation??          
  def execute
    #super cool logic
  end
end
Ofir P
  • 53
  • 1
  • 6
  • Does this answer your question? [How can I disable a validation and callbacks in a rails STI derived model?](https://stackoverflow.com/questions/279478/how-can-i-disable-a-validation-and-callbacks-in-a-rails-sti-derived-model) – engineersmnky Oct 02 '20 at 16:35

2 Answers2

3

Since this is a subclass, you can override the valid method to do something else, or even nothing:

class B < A       
  def execute
    #super cool logic
  end

  private

  def valid
    # Do nothing
  end
end
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
1

You could add a callback for selectively skipping the validation:

class A < ActiveInteraction::Base
  string :s

  validate :valid, unless: :skip_validation
        
  private

  def vaild
    # raise something unless s equals "banana"
  end

  def skip_validation
    false
  end
end


class B < A 
  def execute
    #super cool logic
  end

  private

  def skip_validation
    true # or more fancy logic
  end
end
Stefan
  • 109,145
  • 14
  • 143
  • 218