# Model validation

**URL:** <https://www.ruby-forum.com/t/model-validation/146273>\
**Category:** Rails\
**Created:** [August 19, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/model-validation/146273 "2008-08-19T18:01:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 19, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/model-validation/146273/1 "2008-08-19T18:01:54Z")

</div>

Hi,

I don’t understand the validate method inside the model objet, when  
using the fields are always empty ?

class Personne \< ActiveRecord::Base  
set\_table\_name ‘personnes’

```
has_many :emails

protected

def validate
    if nom = ""
        errors.add( "nom", "Le nom ne peut pas etre vide" )
    end
    if prenom = ""
        errors.add( "prenom", "Le prenom ne peut pas etre vide" )
    end
end

```

end

Thank you for your help

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 19, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/model-validation/146273/2 "2008-08-19T18:19:09Z")

</div>

> ```
> def validate
> if nom = ""
> errors.add( "nom", "Le nom ne peut pas etre vide" )
> end
> if prenom = ""
> errors.add( "prenom", "Le prenom ne peut pas etre vide" )
> end
> end
> 
> ```

You are setting nom equal to blank by using nom = “”. Instead, write  
nom == “” to compare nom to blank. Of course you’ll do the same with  
prenom.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 19, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/model-validation/146273/3 "2008-08-19T21:26:19Z")

</div>

Hey Alex,

you can use the method “validates\_presence\_of” to validate the fields  
in blank:

class Personne \< ActiveRecord::Base  
set\_table\_name ‘personnes’

```
has_many :emails

validates_presence_of :nom, :message => "Le nom ne peut pas etre

```

vide"  
validates\_presence\_of :prenom, :message =\> “Le prenom ne peut pas  
etre vide”  
end

I hope I help you.

On Aug 19, 6:19 pm, “Christina B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 9:08am UTC](https://www.ruby-forum.com/t/model-validation/146273/4 "2008-08-20T09:08:56Z")

</div>

Thank you a lot !

No comment about the = and ==, this is the result of working being  
tired…
