activerecords validation in ruby on rails showing multiple outputs - ruby-on-rails

I want to validate the presence of a filed called age. Age can not be blank and should be a number.
Here is my code snipped
validates_presence_of :age
validates :age, numericality: true
its working fine .But my problem is when age is blank output shows
age can't be blank
age is not a number
i don't want to print "age is not a number" when age is blank

You can use allow_blank: true to skip the validation on numericality when the age is blank.
validates :age, numericality: true, allow_blank: true
You can further merge those two validations into one
validates :age, presence: true, numericality: {allow_blank: true}
There is a similar article here

try this
validates_presence_of :age
validates :age, numericality: true, allow_blank: true

Related

Ruby - Validates field presence after create

Is it possible to validate a field for presence after the initial creation?
I want to make phone number mandatory if the user wants to update their account after signing up.
validates :phone, presence: true, if: .....
if I use on: :update I can no longer authenticate until the field is filled
There are many ways to accomplish this task assuming it is a normal Rails model backed by a DB table. Off the top of my head you can do:
validates :phone,
presence: true,
if: Proc.new{ |model| model.id.present? }
Or more to the point and doesn't fail if you assign an ID before saving:
validates :phone,
presence: true,
if: Proc.new{ |model| model.persisted? }

Make Rails Validation Helpers uniqueness multiple with a group of columns

How i can validate four attributes at once using method uniqueness or if I have how?
I am use MySQL:
class Product < ApplicationRecord
validates :manufacturer,:model, :color,:carrier_plan_type, :quantity, :price, presence: true
validates :quantity, numericality: {greater_than_or_equal_to: 0}
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :manufacturer, confirmation: { case_sensitive: false }
validates :model, format: {with: /\w+\s\w{2}\s\d{2,3}GB/i }
validates :color, format: {with: /[a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+/i}
validates :carrier_plan_type, format: {with: /p(re|os)/}
end
This is my model Product, i would like to 4 columns (manufacturer, model, color and carrier_plan_type) with method_uniqueness. So that it is only valid to insert an instance in my database if you do not have these 4 columns repeated.
I found in Rails Guide that:
See the MySQL manual for more details about multiple column indexes or the PostgreSQL manual for examples of unique constraints that refer to a group of columns.
But i didn't understand how i can to group the columns.
To add, It is a good practice to do the validations as I did up there, if you have another opinion I am open to an idea.
Try using:
validates_uniqueness_of :carrier_plan_type, scope: [:manufacturer, :model, :color]
or
validates :carrier_plan_type, uniqueness: { scope: [:manufacturer, :model, :color] }
either should give you what you want.
In reply to the comment below try:
validates_uniqueness_of :carrier_plan_type,
scope: [:manufacturer, :model, :color, :price], if: -> { quantity == 0 }
or even
validates_uniqueness_of :carrier_plan_type,
scope: [:manufacturer, :model, :color, :price], if: :quantity_zero
private
def quantity_zero
quantity == 0
end

What does this validation do?

I know this validation checks for the presence of an e-mail before rendering an object as valid (in this case an user)
validates :email, presence: true
But this one I have no idea what it does
validates :login, :email, presence: true
It validates both :login and :email using the PresenceValidator, same as writing:
validates :login, presence: true
validates :email, presence: true
It add a presence validation to email and login.
validates takes a list of attributes you want to validate and a hash containing the validations you want to apply. Which is handy if you want to apply the same validations to several attributes.
These are all equivalent:
validate_presence_of :email, presence: true
validate_presence_of :login, presence: true
# or
validates :email, presence: true
validates :login, presence: true
# or
validates :login, :email, presence: true

Ruby on Rails 4. Model conditional validations

I have model with some validation rules:
class Order < ActiveRecord::Base
validates :zip_code, presence: true, length: {is: 5}, numericality: {only_integer: true, :greater_than => 0}
end
And when zip_code is blank i don't need to perform other zip_code validations (it is redundant and all other validation messages on user page look very strange if zip_code is blank)
How can i implement this logic? i need to validate length, is_integer and greater_than only if zip_code is not blank? and i need to show only zip_code can't be blank message on user page
You can do something like
validates :zip_code, presence: true
validates :zip_code, length: {is: 5}, numericality: {only_integer: true, :greater_than => 0}, :if => :zip_code?
Hope it helps!

Rails 4 Validation: where to put the allow_nil when doing an inclusion?

Are these two implementations functionally equivalent? If so, which is "better?"
# from a model
WIDGET_COLORS = %w(red yellow green)
validates :widget_color,
inclusion: {in: WIDGET_COLORS, allow_nil: true}
or
# from a model
WIDGET_COLORS = %w(red yellow green)
validates :widget_color,
inclusion: {in: WIDGET_COLORS},
allow_nil: true
UPDATE: fixed typo so example reads validates
Firstly validate and validates are different methods - it should be validates here.
validates will search the supplied hash for so-called _validates_default_keys, which is an internal array [:if, :unless, :on, :allow_blank, :allow_nil , :strict]. All the arguments passed to validates being in this array are treated as common options for all the validators attached to the model with this method. So if you do:
validates :widget_color,
inclusion: {in: WIDGET_COLORS},
uniqueness: true,
allow_nil: true
allow_nil will affect both of the validators, or is equivalent of:
validates :widget_color,
inclusion: {in: WIDGET_COLORS, allow_nil: true},
uniqueness: {allow_nil: true}
On the other hand with
validates :widget_color,
inclusion: {in: WIDGET_COLORS, allow_nil: true},
uniqueness: true
it will only affect the validator it is defined for (in this case InclusionValidator)

Resources