2
複数のフィールドに基づいてレコードの一意性を検証する検証を書くことができるかどうかを知りたいと思います。私のモデルに複合主キーがあります。つまり、ActiveRecordのコンポジットキーでモデルを検証する方法は?
field :houseno, :type => String
field :street, :type => String
field :boro, :type => String
このレコードの一意性を検証する良い方法はありますか?
私はこのようなカスタムバリデータを使用しようとしている:
class AddressValidator < ActiveModel::Validator
def validate(record)
record.errors[:base] << "This address is already in our records." unless check(record)
end
private
def check(record)
Address.find(:street=>record.street,:houseno=>record.houseno,:boro=>record.boro).length > 0
end
end
ありがとうございます。 houseno、:length => {:minimum => 1、:maximum => 6、:allow_blank => false、:allow_nil => true}:一意性=> {:scope => [通り、:boro]} – Avishai