# How can I conditional the validation on a model validation?

**URL:** <https://www.ruby-forum.com/t/how-can-i-conditional-the-validation-on-a-model-validation/185101>\
**Category:** Rails\
**Created:** [March 13, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/how-can-i-conditional-the-validation-on-a-model-validation/185101 "2010-03-13T19:20:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 13, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/how-can-i-conditional-the-validation-on-a-model-validation/185101/1 "2010-03-13T19:20:39Z")

</div>

I’m making an application in Ruby, but I create records from text files,  
which contain basic information to create a record. Then I modify the  
records from the website, but now validating all fields.  
What I do is validate some fields when the record is created from the  
text file, and then validate the other fields which are filled from the  
website.  
Here’s an example:

# My Table

Table name: Person  
Fields: id, name, address, telephone, mail

The text file only contains the id and name. From the website, add the  
other fields, but I also want to validate.

So I have the model:

class Person \<ActiveRecord:: Base  
validates\_presence\_of: id  
validates\_presence\_of name  
end

How can I make previous validation from the model?

---

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 13, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/how-can-i-conditional-the-validation-on-a-model-validation/185101/2 "2010-03-13T21:40:14Z")

</div>

On 13 March 2010 18:20, Jorge alejandro Mendoza torres  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fields: id, name, address, telephone, mail
> 
> The text file only contains the id and name. From the website, add the  
> other fields, but I also want to validate.
> 
> So I have the model:
> 
> class Person \<ActiveRecord:: Base  
> validates\_presence\_of: id  
> validates\_presence\_of name

Is this what you want?  
validates\_presence\_of :somethingelse, :on =\> :update

See the docs for validations at  
[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)

Colin

---

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 14, 2010, 12:30pm UTC](https://www.ruby-forum.com/t/how-can-i-conditional-the-validation-on-a-model-validation/185101/3 "2010-03-14T12:30:55Z")

</div>

If you’re asking “how do I avoid the validation check on name and id  
when  
updating”, then you should look at :on =\> params for validation.

validates\_presence\_of :id, :name, :on =\> :create

I think everything is clearly explained in the docs.

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)  
[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)

On 13 March 2010 19:20, Jorge alejandro Mendoza torres  
\<removed\_email\_address@domain.invalid

> wrote:

> Fields: id, name, address, telephone, mail
> 
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
100% naturally selected. 0% designed.
