简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM