2011-03-08 6 views
0

막혔습니다. 단순한 유효성 검사가 작동하는 것처럼 보이기 위해 노력하고 있지만, 여전히 작동하도록 할 수는 없습니다. 과정은 '라이브'를 선택, 다음 빈 도시가 저장 될 수 있도록 아무것도 가지고 있지만 경우, 내가하고 싶은 무엇레일 서버 측 유효성 검사 - belongs_to 모델

class Course < ActiveRecord::Base 

has_many :locations, :dependent => :destroy 
accepts_nested_attributes_for :locations, :allow_destroy => true 

end 

class Location < ActiveRecord::Base 

    belongs_to :course 

    validates_presence_of :city, 
        :message => "City cannot be blank", 
        :allow_blank => true, 
        :if => Proc.new {|location| location.nil? || (!location.nil? && location.course.format != 'Live') } 

end 

은 다음과 같습니다

모델 : 여기

는 설정이다 위치 테이블 그렇지 않으면 course.format이 'Live'인 경우 오류를 던집니다.

나는 그것이 정의 된 위치가없는 곳에 유효성 검사를하고 있다고 생각한다. 그러나 코스를 업데이트하고 형식을 '라이브'로 변경하면 유효성 검사가 제대로 작동하지 않습니다.

레일에서 내가 위에있는 것과 같은 관련 테이블의 다른 컬럼을 어떻게 참조합니까? 새로운 과정 인 경우 검증 과정을 확인하는 동안 과정이 아직 생성되지 않았으므로 물론 그렇지 않습니다. 사실이다.

미리 감사드립니다.

답변

0
 
validates_presence_of :city, 
        :message => "City cannot be blank", 
        :allow_blank => true, 
        :if => Proc.new {|location| location.nil? || (!location.nil? && location.course.format != 'Live') } 

위 유효성 확인에서 allow_blank는 if 블록과 관련이 없습니다. 시도해주세요.

 
validates_presence_of :city, 
        :message => "City cannot be blank", 
        :if => Proc.new {|location| 'your_conditions'} 

Now this validation works when 'your_condition' will return true. 

관련 문제