의 유효성에 다른 경우 아래레일 모델에서
validates :category, :presence => true if self.post = "this_is_post"
같은 그것이 가능이나 내가 전에 후크 방법을 사용하여이 검사에 저장해야?
의 유효성에 다른 경우 아래레일 모델에서
validates :category, :presence => true if self.post = "this_is_post"
같은 그것이 가능이나 내가 전에 후크 방법을 사용하여이 검사에 저장해야?
이 작동합니다 :
여기validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' }
, 레일에서 유효성 검사 조건에 대해 하나 이상의 코드 snippest :
class Person < ActiveRecord::Base
validates :surname, presence: true, if: "name.nil?"
end
=============== =========
validates :category,
:presence => true,
:if => :valid?
def valid?
self.post == "this_is_post"
end
================
class Person < ActiveRecord::Base
validates :category, presence: true, if: "self.post.eql?('this_is_post')"
end
마지막 하나가 작동하지 않지만 마지막 하나와 비슷한 것을 찾고 있습니다. – asdfkjasdfjk
여기에 무엇이 있는지 설명해주십시오. 모델에서는 self.post를 사용하여 모든 리소스 필드에 액세스합니다. – asdfkjasdfjk
@ user3128796이': if' 옵션에서'Proc' 객체를 넘겨 주면, 여러분의 모델 객체로 전달됩니다 (예제에서'a'로 참조). 그래서 당신은 단순히 'post'를 호출 할 수 있습니다.'regular '객체를 사용하는 것처럼 말입니다. –
@ user3128796 아직 불분명 한 부분이 있습니까? –