자체 또는 중첩 된 속성에 대한 유효성 검사를 실행하지 않고 레코드를 저장할 수 있어야합니다. 레일즈 3.0에서 멈추었 고 새로운 버전으로 업데이트 할 수 없습니다.Rails 3.0 중첩 된 속성의 유효성 검사 건너 뛰기
각 보고서에는 많은 답변 (질문에 대한 답변)이 있습니다. 응답은 보고서 양식에 중첩됩니다.
사용자가 보고서를 저장할 수 있어야하는 방법에는 검토 용으로 제출, 모든 유효성 검사가 실행되는 곳, 저장 및 마침 나중에 보고서 또는 중첩 된 응답에 대해 유효성 검사가 실행되지 않는 두 가지가 있습니다. 이는 작성 및 갱신 조치 모두에 대해 작동해야합니다.
현재 조건부 유효성 검사를 사용하려고합니다. 이 작업은 업데이트는 가능하지만 만들 수는 없습니다. 문제는 다음과 같습니다.
validate :has_answer_if_required, :if => Proc.new { |response| !response.report.finish_later? }
보고서가 아직 존재하지 않으므로 활성 레코드가이 응답의 보고서를 찾을 수 없습니다. 그것은 그것이 부서지는 곳이다.
이 문제에 대한 몇 가지 제안 된 해결책이 있지만 레일즈 3.0에서 작동하지 못했습니다. 예를 들어, update_attributes (attributes, : validate => false)는 Rails 3.0에서 사용할 수 없습니다.
따라서 중첩 된 특성의 유효성 검사를 어떻게 건너 뛰 시나요?
https://github.com/npearson72/validation_skipper
것은 그냥 당신이 개체 당신 skip_validation_for
를 호출해야합니다 : 그것은 내가 그것을해야한다고 생각하지만, 중첩 된 속성 작업 ...하지만 ValidationSkipper에게 시험을 줄 것이다 경우
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
http://stackoverflow.com/questions/9529766/skip-validations-for-nested-attributes-in-rails-3 –