Ruby-on-rails – Rails ActiveRecord: validate single attribute

rails-activerecordruby-on-railsvalidation

Is there any way I can validate a single attribute in Rails?

Something like:

ac_object.valid?(attribute_name)

Best Answer

You can implement your own method in your model. Something like this

def valid_attribute?(attribute_name)
  self.valid?
  self.errors[attribute_name].blank?
end

Or add it to ActiveRecord::Base