3

When I add a validation to a singleton_class it seems to get assigned to the base class instead and it doesn't trigger for either.

class Example
  attr_accessor :title, :some_boolean
  include ActiveModel::Validations
end


puts Example.validators  # []

with_validations = Example.new
with_validations.singleton_class.send :validates, :title, :presence => true

puts with_validations.valid?  #true

puts Example.validators.length # 1

What I'd like to happen:

with_validations.valid?  # false
Example.new.valid?       # true
Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52

1 Answers1

0

You can do validation by using 'on' option.

validates :title, presence: true, on: :draft

example = Example.new
example.valid? # true
example.valid?(:draft) #false
galulex
  • 146
  • 3
  • 12