添加模型验证{Rails}

时间:2018-05-14 19:12:04

标签: ruby-on-rails ruby model

我有两个模型关联如下: 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的存在?我怎么能实现这个? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

我不确定您是在寻找基于您的示例中使用if的{​​{1}}还是unless,但请尝试以下操作:

!=