我有两个模型关联: Survey_question和survey_answer。
Survey_question模型
class SurveyAnswer < ApplicationRecord
belongs_to :survey_question
validates_presence_of :answer_content, if: "answer_type!=Radio Button"
endSurvey_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我只是想验证一下,当answer_content _ answer_type是"Radio“时,是否存在answer_type?我怎样才能做到这一点?提前谢谢。
发布于 2018-05-14 19:22:05
我不确定您是基于在示例中使用if还是unless来查找!=,但请尝试以下操作:
validates_presence_of :answer_content, if: ->(answer) { answer.survey_question.answer_type == 'Radio Button' }
https://stackoverflow.com/questions/50337449
复制相似问题