# How to bypass model validation

**URL:** <https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507>\
**Category:** Rails\
**Created:** [August 21, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507 "2008-08-21T20:40:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arslan\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arslan\_Ali](https://www.ruby-forum.com/u/Arslan_Ali)\
**Post date:** [August 21, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507/1 "2008-08-21T20:40:27Z")

</div>

I have a model named as user, this model have check of password between  
length 6 to 16. But there are some scenarios where i cannot provide  
password  
while creating instance of user model. Here i am unable to save partial  
entries in database because validation forbids me.

I want to know is there any to bypass validation checks in models

---

<div class="post-metadata">

**Author:** ![Arslan\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arslan\_Ali](https://www.ruby-forum.com/u/Arslan_Ali)\
**Post date:** [August 21, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507/2 "2008-08-21T20:41:38Z")

</div>

On Aug 21, 7:39 pm, “Arslan Ali” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a model named as user, this model have check of password between  
> length 6 to 16. But there are some scenarios where i cannot provide password  
> while creating instance of user model. Here i am unable to save partial  
> entries in database because validation forbids me.

> I want to know is there any to bypass validation checks in models  
> well if you pass false to save it won’t validate, or you can use  
> the :if/:unless options on the validations to control when they run.

Fred

---

<div class="post-metadata">

**Author:** ![Arslan\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arslan\_Ali](https://www.ruby-forum.com/u/Arslan_Ali)\
**Post date:** [August 21, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507/3 "2008-08-21T20:47:29Z")

</div>

Here’s how I’ve done it.

attr\_accessor :step # to hold the step in the process

with\_options :if =\> Proc.new{|record| record.step =\> 2} do |r|  
r.validates\_presence\_of :credit\_card, :ccv\_code, :expiration\_date  
r.validates\_numericality\_of :credit\_card

### other validations

end

On Thu, Aug 21, 2008 at 1:40 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![Arslan\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arslan\_Ali](https://www.ruby-forum.com/u/Arslan_Ali)\
**Post date:** [August 21, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507/4 "2008-08-21T22:39:28Z")

</div>

Arslan Ali wrote:

> I have a model named as user, this model have check of password between  
> length 6 to 16. But there are some scenarios where i cannot provide  
> password  
> while creating instance of user model. Here i am unable to save partial  
> entries in database because validation forbids me.
> 
> I want to know is there any to bypass validation checks in models

I don’t know if you are using validates\_format\_of like I am but here is  
an example of what I did:

validates\_format\_of :password, :with =\> /^\w+$/,  
:message =\> ‘**Password must be  
alphanumeric**’,  
:on =\> :create

-S

---

<div class="post-metadata">

**Author:** ![Arslan\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arslan\_Ali](https://www.ruby-forum.com/u/Arslan_Ali)\
**Post date:** [August 21, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-bypass-model-validation/146507/5 "2008-08-21T20:48:04Z")

</div>

and of course there’s a typo.

with\_options :if =\> Proc.new{|record| record.step =\> 2} do |r|

should be

with\_options :if =\> Proc.new{|record| record.step == 2} do |r|

☹
