Validates_..., :unless not working for me (rails 1.2.6)

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?

On Mon, Jul 7, 2008 at 2:45 PM, Constantin G.
[email protected] 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


http://lindsaar.net/
Rails, RSpec and Life blog…

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”]

On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.
[email protected] 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


http://lindsaar.net/
Rails, RSpec and Life blog…

Constantin G. wrote:

Mikel L. wrote:

On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.
[email protected] 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.

Mikel L. wrote:

On Mon, Jul 7, 2008 at 3:41 PM, Constantin G.
[email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs