# Validates\_..., :unless not working for me (rails 1.2.6)

**URL:** <https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350>\
**Category:** Rails\
**Created:** [July 7, 2008, 6:45am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350 "2008-07-07T06:45:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 7, 2008, 6:45am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/1 "2008-07-07T06:45:33Z")

</div>

class PayableInvoice \< Invoice  
validates\_presence\_of :number, :unless =\> :importing

attr\_accessor :importing

def after\_create  
update\_attribute(:number, id) if importing  
end

end

> > p = PayableInvoice.new(:importing =\> true)  
> > p.valid?  
> > =\> false
> 
> > p.errors.full\_messages.to\_a  
> > =\> [“Number can’t be blank”]

Why the bypass validation doesn’t work? Commenting the line works so I’m  
not editing the wrong file.

> > p.importing  
> > =\> true

Importing is true, so it should bypass, right? It doesn’t. Anyone has  
any idea why?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 7, 2008, 7:25am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/2 "2008-07-07T07:25:42Z")

</div>

On Mon, Jul 7, 2008 at 2:45 PM, Constantin G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Importing is true, so it should bypass, right? It doesn’t. Anyone has  
> any idea why?

From the ActiveRecord RDoc:

- unless - Specifies a method, proc or string to call to determine if  
the validation should not occur (e.g. :unless =\> :skip\_validation, or  
:unless =\> Proc.new { |user| user.signup\_step \<= 2 }). The method,  
proc or string should return or evaluate to a true or false value.

So in your case, probably :unless =\> Proc.new { |pi| pi.importing? }

Validations are class methods and are called out of scope of your  
actual instance. The class method is called with 'hey, is instance  
xyz cool?" and the class method replies yes or no. So you need to  
tell it what to test ‘importing’ on…

Hope that helps

Mikel

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 7, 2008, 7:41am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/3 "2008-07-07T07:41:59Z")

</div>

Mikel L. wrote:

> So in your case, probably :unless =\> Proc.new { |pi| pi.importing? }
> 
> Validations are class methods and are called out of scope of your  
> actual instance. The class method is called with 'hey, is instance  
> xyz cool?" and the class method replies yes or no. So you need to  
> tell it what to test ‘importing’ on…

I didn’t look at the rails code for this, but if :importing were to be  
called as a class method it would have been failed, because I don’t have  
a class method “importing”. This might be a clue that it does not even  
go to :unless.

Anyway, I tried the “instance” way too. The same result.

class PayableInvoice \< Invoice  
validates\_presence\_of :number, :unless =\> Proc.new{|inv|inv.importing}  
attr\_accessor :importing  
end

> > p = PayableInvoice.new(:importing =\> true)  
> > p.valid?  
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/validations  
> > .rb:74: warning: Object#type is deprecated; use Object#class  
> > =\> false
> 
> > p.errors.full\_messages.to\_a  
> > =\> [“Number can’t be blank”]

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 7, 2008, 7:53am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/4 "2008-07-07T07:53:12Z")

</div>

On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a class method “importing”. This might be a clue that it does not even  
> go to :unless.

Well, really because ‘importing’ in that scope would resolve to nil.  
So you would have ‘unless nil’

This is because it could be a method or a locally defined variable.  
Ruby won’t pick you up on this error.

> =\> false
> 
> > > p.errors.full\_messages.to\_a  
> > > =\> [“Number can’t be blank”]

Then you have some other method in the parent class that is stuffing  
things up. The Object#Type warning is a pointer towards this…  
something else is wrong.

I just tried it on a dummy model here:

class User \< ActiveRecord::Base  
validates\_presence\_of :number, :unless =\> Proc.new { |i| i.importing }  
attr\_accessor :importing  
end

baci:tracker mikel$ ./script/console  
Loading development environment (Rails 2.1.0)

> > u = User.new  
> > =\> #\<User id: nil, created\_at: nil, updated\_at: nil\>  
> > u.valid?  
> > =\> false  
> > u.importing = true  
> > =\> true  
> > u.valid?  
> > =\> true  
> > u = User.new(:importing =\> true)  
> > =\> #\<User id: nil, created\_at: nil, updated\_at: nil\>  
> > u.valid?  
> > =\> true

Works fine.

Have a look at the invoice class. Do you touch importing or number  
there?

Mikel

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 8, 2008, 1:25am UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/5 "2008-07-08T01:25:52Z")

</div>

Constantin G. wrote:

> Mikel L. wrote:
> 
> > On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > a class method “importing”. This might be a clue that it does not even  
> > > go to :unless.
> > 
> > Well, really because ‘importing’ in that scope would resolve to nil.  
> > So you would have ‘unless nil’
> > 
> > This is because it could be a method or a locally defined variable.  
> > Ruby won’t pick you up on this error.
> 
> I changed the :unless to :if and it works. I’m using Rails 1.2.6 for  
> that project and it supports only :if. When I saw that you’re using  
> Rails 2.1.0 and it works for you I realized that is gotta be a new  
> feature.
> 
> class PayableInvoice \< Invoice  
> validates\_presence\_of :number, :if =\> Proc.new{|inv|!inv.importing}  
> attr\_accessor :importing  
> end  
> yeah, makes sense.

If you’re dubious that an options is being used, try doing a  
validates\_presence\_of :number, :unless =\> lambda{|inv| raise(Exception,  
“I dont believe this code gets hit”)}

or something.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [July 7, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/validates-unless-not-working-for-me-rails-1-2-6/142350/6 "2008-07-07T23:08:28Z")

</div>

Mikel L. wrote:

> On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > a class method “importing”. This might be a clue that it does not even  
> > go to :unless.
> 
> Well, really because ‘importing’ in that scope would resolve to nil.  
> So you would have ‘unless nil’
> 
> This is because it could be a method or a locally defined variable.  
> Ruby won’t pick you up on this error.

I changed the :unless to :if and it works. I’m using Rails 1.2.6 for  
that project and it supports only :if. When I saw that you’re using  
Rails 2.1.0 and it works for you I realized that is gotta be a new  
feature.

class PayableInvoice \< Invoice  
validates\_presence\_of :number, :if =\> Proc.new{|inv|!inv.importing}  
attr\_accessor :importing  
end
