다형성 관계에 대해 범위가 지정된 하위 모델의 속성의 고유성을 검증 할 수 있습니까?레일 AR validates_uniqueness_of 다형성 관계
class Field < ActiveRecord::Base
belongs_to :fieldable, :polymorphic => :true
validates_uniqueness_of :name, :scope => :fieldable_id
end
나는 많은 필드가 여러 가지 다른 모델 (페이지, 항목)가 :
예를 들어 나는 fieldable
에 속하는 field
라는 모델을 가지고있다. 그래서 내가 원하는 것은 상위 모델에 대한 필드 이름의 고유성을 검증하는 것이지만 문제는 때때로 페이지와 항목이 동일한 ID 번호를 공유하여 유효성 검증이 실패 할 때 실패하게하는 것입니다.
나는 이것을 잘못하고있는 것일까, 아니면 더 좋은 방법이 있을까? A와
class Field < ActiveRecord::Base
belongs_to :fieldable, :polymorphic => :true
validates_uniqueness_of :fieldable_id, :scope => [:fieldable_id, :fieldable_type], :message => 'cannot be duplicated'
end
: 유효성 검사를 추가 할 또한 기본 메시지를 무시하거나 범위를 사용하는 메시지를 추가 할 수 있습니다
class Field < ActiveRecord::Base
belongs_to :fieldable, :polymorphic => :true
validates_uniqueness_of :name, :scope => [:fieldable_id, :fieldable_type]
end
감사합니다. Simples 당신이 방법을 알고있을 때 :) – aaronrussell