Mikel Lindsaar 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.
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.
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"]
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