Constantin Gavrilescu
2008-Jul-07 04:45 UTC
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mikel Lindsaar
2008-Jul-07 05:25 UTC
Re: validates_..., :unless not working for me (rails 1.2.6)
On Mon, Jul 7, 2008 at 2:45 PM, Constantin Gavrilescu <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> class PayableInvoice < Invoice > validates_presence_of :number, :unless => :importing > > attr_accessor :importing > > def after_create > update_attribute(:number, id) if importing > end > > end > 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.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Constantin Gavrilescu
2008-Jul-07 05:41 UTC
Re: validates_..., :unless not working for me (rails 1.2.6)
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. 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"] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mikel Lindsaar
2008-Jul-07 05:52 UTC
Re: validates_..., :unless not working for me (rails 1.2.6)
On Mon, Jul 7, 2008 at 3:41 PM, Constantin Gavrilescu <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 -- http://lindsaar.net/ Rails, RSpec and Life blog.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Constantin Gavrilescu
2008-Jul-07 21:08 UTC
Re: validates_..., :unless not working for me (rails 1.2.6)
Mikel Lindsaar wrote:> On Mon, Jul 7, 2008 at 3:41 PM, Constantin Gavrilescu > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-Jul-07 23:25 UTC
Re: validates_..., :unless not working for me (rails 1.2.6)
Constantin Gavrilescu wrote:> Mikel Lindsaar wrote: >> On Mon, Jul 7, 2008 at 3:41 PM, Constantin Gavrilescu >> <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > endyeah, 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---