将模型的布尔值设置为字符串会使其为false

时间:2014-06-28 19:44:55

标签: ruby-on-rails

我有一个Answer,其response属性是一个布尔值。如果我将answer.response设置为'abcde',则响应设置为false

class Answer < ActiveRecord::Base
  belongs_to :answer_sheet
  belongs_to :question

  validates :response, inclusion: [true, false]
end

class AnswersController < ApplicationController
  def create
    answer = Answer.find_or_initialize_by(
      answer_sheet_id: answer_params[:answer_sheet_id],
      question_id: answer_params[:question_id]
    )
    answer.response = answer_params[:response]
    answer.save

    render json: answer, status: :created
  end
end

知道为什么会这样吗?我很沮丧。我原以为验证失败了。

0 个答案:

没有答案