I have a Author class and need to make some validations before initializing
class Author
include Validations
attr_accessor :name, :biography
def initialize(name, biography = nil)
validate_author
@name = name
@biography = biography
end
def to_s
"#{name}\n#{biography}"
end
end
I use module for this
module Validations
def validate_author
raise ::StandardError, 'Name is required' if name.strip.empty?
end
end
And i get this error
extentions/validations.rb:8:in `validate_author': undefined method `strip' for nil:NilClass (NoMethodError)
It's ruby application
ruby-on-rails. I wonder why you are implementing validations yourself and do not useActiveModel::Validations?