Rails 3.0跳过嵌套属性的验证

时间:2012-05-30 19:15:35

标签: ruby-on-rails-3 validation nested-attributes

我需要能够保存记录而不对自身或其嵌套属性运行验证。我被困在Rails 3.0中,我无法更新到更新的版本。

我有一份报告,每份报告都有很多回复(问题的答案)。响应嵌套在报告表单中。

用户应该有两种方法可以保存报告:提交以供审核,其中运行所有验证,以及稍后保存并完成,其中不对报告或嵌套响应运行验证。这需要适用于创建和更新操作。

我目前正在尝试使用条件验证。这适用于更新但不能创建。问题是这一行:

validate :has_answer_if_required, :if => Proc.new { |response| !response.report.finish_later? }

该报告尚不存在,因此有效记录无法找到此回复的报告。这就是它崩溃的地方。

这个问题有很多建议的解决方案,但我无法让它们在Rails 3.0中运行。例如,update_attributes(attributes,:validate => false)在Rails 3.0中不可用。

那么,如何跳过嵌套属性中的验证?

class Report < ActiveRecord::Base
  has_many :responses, :order => "created_at asc", :autosave => true
  accepts_nested_attributes_for :responses
  ...
end

class Response < ActiveRecord::Base
  belongs_to :report
  validates_associated  :report

  validate :has_answer_if_required, :if => Proc.new { |response| !response.report.finish_later? }
  validate :correct_answer_or_comment, :if => Proc.new { |response| !response.report.finish_later? }
end

class ReportsController < BaseController

  def update
    @report = Report.find(params[:id])
    @report.attributes = params[:report]

    if params[:finish_later].nil?
      @report.update_attribute(:finish_later, false)
      if @report.save!
      redirect_to :action => :index
    else
      render :template => "reports/edit"
    end
    else
      @report.finish_later = true
      @report.save(:validate => false)
      redirect_to :action => :index
    end
  end

  def create
    @report = Report.new(params[:report])
    if params[:finish_later].nil?
      @report.finish_later = false
      if @report.save!
        redirect_to :action => :index
      else
        render :template => "reports/edit"
      end
    else
      @report.finish_later = true
      @report.save!(:validate => false)
      redirect_to :action => :index
    end
  end
end

1 个答案:

答案 0 :(得分:0)

不确定它是否适用于嵌套属性,但我认为它应该......但是尝试ValidationSkipper:

https://github.com/npearson72/validation_skipper

只需确保在要跳过的对象上调用skip_validation_for即可。由于嵌套属性将行为传递给其子级,因此您可以直接在父对象上调用此方法。试一试。