From 869c874e21157b727cf7e113835767548f7db55c Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 22 Oct 2025 17:16:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Amax=5Fchoice=5Fvote?= =?UTF-8?q?=5Foptions=5Fcount=E5=8F=AF=E4=BB=A5=E4=B8=BAnil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/vote.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index 96f987930..82a639993 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -7,7 +7,16 @@ class Vote < ApplicationRecord accepts_nested_attributes_for :vote_options, allow_destroy: true - validates :max_choice_vote_options_count, numericality: { greater_than: 0 } + validates :max_choice_vote_options_count, + numericality: { + greater_than: 1, + less_than_or_equal_to: 20, + allow_nil: true + }, + if: :is_multiple_choice? + + validate :validate_multiple_choice_settings + def expired? due_at.present? && due_at < Time.current @@ -53,4 +62,14 @@ class Vote < ApplicationRecord return 0 if total.zero? (option_votes.to_f / total * 100).round(2) end + + def validate_multiple_choice_settings + if is_multiple_choice? && max_choice_vote_options_count.nil? + errors.add(:max_choice_vote_options_count, '多选投票必须设置最大选择数') + end + + if !is_multiple_choice? && max_choice_vote_options_count.present? + errors.add(:max_choice_vote_options_count, '单选投票不需要设置最大选择数') + end + end end