# Skip validation when this\_action

**URL:** <https://www.ruby-forum.com/t/skip-validation-when-this-action/140968>\
**Category:** Rails\
**Created:** [June 20, 2008, 9:34am UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968 "2008-06-20T09:34:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 9:34am UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/1 "2008-06-20T09:34:06Z")

</div>

How can I write a condition in a validation that will skip validation if  
it comes from a certain action?

:if =\> don’t\_validate\_when\_this\_action

Or how do you handle this, with validation, when you have an update form  
that are for updating certain fields/columns?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 11:31am UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/2 "2008-06-20T11:31:37Z")

</div>

On Jun 20, 8:34 am, Pål Bergström [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How can I write a condition in a validation that will skip validation if  
> it comes from a certain action?
> 
> :if =\> don’t\_validate\_when\_this\_action
> 
> Or how do you handle this, with validation, when you have an update form  
> that are for updating certain fields/columns?

The model has no idea what action has caused it to be validated.  
Either call save(false) which skips all validations, or let the model  
know (eg have an instance variable which indicates which validations  
can be skipped)

Fred

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/3 "2008-06-20T12:05:22Z")

</div>

On 20 Jun 2008, at 10:46, PÃ¥l BergstrÃ¶m wrote:

> > >
> 
> Do you mean save instead of update\_attributes?
> 
> How do I pick up that instance variable in the model? I can’t use  
> params[:variable], right? Is it :variable?  
> not sure what you mean there.

Fred

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 11:46am UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/4 "2008-06-20T11:46:49Z")

</div>

Frederick C. wrote:

> On Jun 20, 8:34ï¿½am, Pï¿½l Bergstrï¿½m [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > How can I write a condition in a validation that will skip validation if  
> > it comes from a certain action?
> > 
> > :if =\> don’t\_validate\_when\_this\_action
> > 
> > Or how do you handle this, with validation, when you have an update form  
> > that are for updating certain fields/columns?
> 
> The model has no idea what action has caused it to be validated.  
> Either call save(false) which skips all validations, or let the model  
> know (eg have an instance variable which indicates which validations  
> can be skipped)
> 
> Fred

Do you mean save instead of update\_attributes?

How do I pick up that instance variable in the model? I can’t use  
params[:variable], right? Is it :variable?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/5 "2008-06-20T12:19:17Z")

</div>

still a bit confusing.

> Do you mean save as if I use a @object.save in the update action?

Is this the right way to write it?

def update  
@object = Object.find(id)  
@object.update\_attributes(params[:object])  
@object.save(false)  
end

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/6 "2008-06-20T12:13:47Z")

</div>

Frederick C. wrote:

> On 20 Jun 2008, at 10:46, PÃ¥l BergstrÃ¶m wrote:
> 
> > > >
> > 
> > Do you mean save instead of update\_attributes?
> > 
> > How do I pick up that instance variable in the model? I can’t use  
> > params[:variable], right? Is it :variable?  
> > not sure what you mean there.
> 
> Fred

Neither do I 🙂 I’ve been using Rails for a while now, but some stuff  
is still a bit confusing.

Do you mean save as if I use a @object.save in the update action?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 20, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/skip-validation-when-this-action/140968/7 "2008-06-20T12:40:42Z")

</div>

On 20 Jun 2008, at 11:19, Pål Bergström wrote:

> @object.save(false)  
> Not quite - update\_attributes does the save for you, so this will save  
> twice (although the update\_attributes might fail because of the  
> validation)  
> @object = Object.find(id)  
> @object.attributes = params[:object]  
> @object.save(false)

Should do the trick

Fred
