Sid Brooklyn
Sid Brooklyn

Reputation: 1042

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.

Upvotes: 0

Views: 62

Answers (1)

Prashant4224
Prashant4224

Reputation: 1601

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 }

Upvotes: 1

Related Questions