Rails - Count atribute before save -


i have class aluno has :telefone attribute. want limit same telefone number in 3 times. it's possible have 3 telefone numbers in :telefone column.

before create new aluno, have check if have 3 alunos same telefone.

it "select count(telefone) alunos telefone = '_form.telefone'

if count = 3 message "max 3 telefones reached"

how that?

thanks all!

yes, need create custom validator in model. below.

class aluno < activerecord::base   ...    validate :there_are_three_max_telefone    def there_are_three_max_telefone     alunos = aluno.find_all_by_telefone(telefone)     if alunos.count >= 3       errors[:base] << "max 3 telefones reached"     end   end end 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -