Reputation: 660
Let's say that I want to make a condition to not create a new record if it returns false. My custom validation is not creating a rollback.
user.rb
class User < ApplicationRecord
validate :test # my custom validation
private
def test
# condition to return false
false
end
end
Should I make any other previews configuration? Thank's.
Upvotes: 0
Views: 354
Reputation: 2781
Custom validation should add an error to an array of errors for the model, for example:
def test
errors[:base] << "This user is invalid because ..."
end
The method valid?
checks if there are any errors in errors
array. It does not care about return value from the custom validator.
Upvotes: 1