So how is user object still returning as valid even when I added errors to the base?
This is what valid? does under the hood (docs).
# Runs all the specified validations and returns +true+ if no errors were
# added otherwise +false+.
def valid?(context = nil)
current_context, self.validation_context = validation_context, context
errors.clear
run_validations!
ensure
self.validation_context = current_context
end
As you can see errors.clear is called right before the validations are triggered, that's why it returns true
So how can I make my user object invalid in irb?
The only way I could make it invalid is by forcing an attribute, which has validations on it, to be invalid.
user = User.last
user.valid? # => true
user.email = nil
user.valid? # => false
user.errors.messages # => {:email=>["can't be blank", "is invalid"]}
I see from the comment on the other answer that it's not what you need/want but it's just how it works.