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
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.
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.
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.
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
validates :name, :presence => true, :uniqueness => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With