Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple validations Rails 3 (Rails for Zombies; 2:3)

I am working through Rails for Zombies, loving the helpful tutorial and interested in others by the way....

My issue is as follows.

The tutorial ask me to

"Do both uniqueness and presence validation on a Zombie's name on a single line, using the new syntax"

I have tried the following in the console at RfZ;


class Zombie < ActiveRecord::Base
  validates_uniqueness_of :name, validates_presence_of :name
end

// AND //

class Zombie < ActiveRecord::Base
  validates Name :uniqueness, :presence => true
end

The tutorial is asking for the new Rails 3 syntax. I understand the new syntax allows for multiple validation arguments in one line. Very nice, but how would I do this? Thanks in advance everyone.

-Ryan

like image 545
Ryan Hell Avatar asked Jun 09 '12 02:06

Ryan Hell


People also ask

How do I bypass validation?

A very common way to skip validation is by keeping the value for immediate attribute as 'true' for the UIComponents. Immediate attribute allow processing of components to move up to the Apply Request Values phase of the lifecycle. scenario: While canceling a specific action, system should not perform the validation.

What is the difference between validate and validates in rails?

So remember folks, validates is for Rails validators (and custom validator classes ending with Validator if that's what you're into), and validate is for your custom validator methods.

What are validations in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.


2 Answers

Your second attempt is closer, but not quite correct. Try this:

class Zombie < ActiveRecord::Base
  validates :name, :uniqueness => true, :presence => true
end

FYI, the older syntax would be :

class Zombie < ActiveRecord::Base
  validates_presence_of :name
  validateS_uniqueness_of :name
end
like image 80
Rob d'Apice Avatar answered Oct 18 '22 21:10

Rob d'Apice


validates :name, :presence => true, :uniqueness => true
like image 22
Oscar Del Ben Avatar answered Oct 18 '22 21:10

Oscar Del Ben