简体   繁体   中英

Ruby on Rails validation does not work for two fields in the model

I am new in Ruby and Ruby on Rails, so maybe my question is not appropriate. I am making a Tic Tac Toe game and when I try to validate my player names like this:

  validates_presence_of :player_1, :player_2
  validates_uniqueness_of :player_1
  validates_uniqueness_of :player_2

And do this in my config/initializers/cleaner.rb

Game.destroy_all

my validation does not work. I also tried Ruby on Rails validation by pair of properties , but it does not work. Thanks in advance.

Please try this:

validates : player_1, :presence => true, :uniqueness => { :scope => : player_2 }
validates : player_2, :presence => true, :uniqueness => { :scope => : player_1 }

OR

validates :player_1, uniqueness: { scope: :player_2 }
validates :player_2, uniqueness: { scope: :player_1 }

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