2

:telephone 属性を持つクラス aluno があります。同じ電話番号を3回までに制限したい。:telephone 列に 3 つの電話番号を入れることは可能です。

新しい aluno を作成する前に、同じ電話で既に 3 つの aluno があるかどうかを確認する必要があります。

「SELECT count(telephone) FROM alunos where telephone = '_FORM.telephone'」のようになります。

if count = 3 Message "Max 3 電話にすでに到達しました"

どうすればいいですか?

4

3 に答える 3

0

この検証にはカスタム メソッドを使用します。このようなものが Aluno モデルに入るはずです。

validate :telefone_count

def telefone_count
  tele_count = Aluno.where(telefone: telefone).count
  if tele_count >= 3
    errors.add(:telefone, "Already 3 or more with the same telefone.")
  end
end
于 2013-04-05T04:29:42.907 に答える