I'm on Rails 5.0. I'm confused about how to evalute whether I have a valid model. I have a model with this method
class MyObjectTime < ActiveRecord::Base
...
validate :valid_method
def valid_method
false
end
Notice that I have hard-coded "false" in my valid method, but even when I create an object of this class, and call "obj.valid?", I will get "true" even though I have rigged things so that the model should never be valid. What else do I need to do to get my "valid_method" method considered when determining if a model is valid?
class MyObjectTime < ActiveRecord::Base
...
validate :valid_method
def valid_method
errors.add(:foo, "is not valid") if some_condition?
end
The return value of a validator method does not matter, what matters is that it adds an error to the errors object (which is a hash like object).
Basically .valid?
runs the validations and then checks if self.errors.any?
.