rails: force a model to be not valid

ruby, ruby-on-rails, ruby-on-rails-3, ruby-on-rails-3.1, ruby-on-rails-3.2

Solution

You can do:

validate :forced_to_be_invalid

def make_not_valid!
  @not_valid = true
end

private

def forced_to_be_invalid
  errors.add(:base, 'has been forced to be invalid') if @not_valid
end

Problem

I have a very specific situation where I want to force an instance of a model not valid. Something like this: ``` user = User.new user.valid? #true user.make_not_valid! user.valid? #false ``` Any way to achieve that? Thanks!

Original source