# Advanced activerecord validation

**URL:** <https://www.ruby-forum.com/t/advanced-activerecord-validation/76798>\
**Category:** Rails\
**Created:** [November 21, 2006, 7:46am UTC](https://www.ruby-forum.com/t/advanced-activerecord-validation/76798 "2006-11-21T07:46:57Z")\
**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:** [November 21, 2006, 7:46am UTC](https://www.ruby-forum.com/t/advanced-activerecord-validation/76798/1 "2006-11-21T07:46:57Z")

</div>

Hey everyone,

I was wondering if its possible that something like this:

validates\_presence\_of :image\_1

is only run when in a particular :action.

Petr

---

<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:** [November 21, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/advanced-activerecord-validation/76798/2 "2006-11-21T12:42:55Z")

</div>

petr wrote:

> I was wondering if its possible that something like this:
> 
> validates\_presence\_of :image\_1
> 
> is only run when in a particular :action.

Take a look at the :if parameter in the documentation.

- Roderick

---

<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:** [November 21, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/advanced-activerecord-validation/76798/3 "2006-11-21T14:06:45Z")

</div>

Roderick van Domburg wrote:

> petr wrote:
> 
> > I was wondering if its possible that something like this:
> > 
> > validates\_presence\_of :image\_1
> > 
> > is only run when in a particular :action.
> 
> Take a look at the :if parameter in the documentation.
> 
> - Roderick

I did, however im not any smarter from it! How would you run a  
validation only for action “add” ?

---

<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:** [November 21, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/advanced-activerecord-validation/76798/4 "2006-11-21T14:59:11Z")

</div>

model validations aren’t tied in any way to controller actions, and  
you shouldn’t really have a need to this. what i think you might want  
to use is the :on option

validates\_presence\_of :image\_1, :on =\> …

:save - default, validates anytime model is saved (insert or update)  
:create - only validates when model is created (insert)  
:update - only validates when model is updated (update)
