我有两个模型关联如下: Survey_question和survey_answer。
Survey_question模型
class SurveyAnswer < ApplicationRecord
belongs_to :survey_question
validates_presence_of :answer_content, if: "answer_type!=Radio Button"
end
Survey_question模型
class SurveyQuestion < ApplicationRecord
belongs_to :user
belongs_to :survey
has_many :survey_answers
accepts_nested_attributes_for :survey_answers, allow_destroy: true
has_many :survey_responses
accepts_nested_attributes_for :survey_responses
validates :question_content, presence: true
validates :answer_type, presence: true
end
当survey_question的answer_type是“单选按钮”时,我只是验证survey_answer的answer_content的存在?我怎么能实现这个? 提前谢谢。
答案 0 :(得分:1)
我不确定您是在寻找基于您的示例中使用if
的{{1}}还是unless
,但请尝试以下操作:
!=