Rails custom validation - Only one record can be true

后端 未结 3 1492
野性不改
野性不改 2021-02-05 04:44

I\'m trying to write a validation where only one record can be true. I have a \'game\' model with an \'active\' boolean column, only one game can be active at any time, so if so

3条回答
  •  长情又很酷
    2021-02-05 05:11

    I think you can just check the uniqueness of active_game when it is true.

    validates_uniqueness_of :active_game, if: :active_game

提交回复
热议问题