Is it possible to use the built-in validation helper methods from within my own validate() (or validate_save() or validate_create()) method, or are they limited to only being used as class methods? I need to override validate() but it would still be helpful to take advantage of the existing helpers. Thanks! /afb -- 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 -~----------~----~----~----~------~----~------~--~---
I''m trying to understand this by going through the source, but I
don''t.
I know that these validation helpers are implemented as class methods,
presumably ''extend'' is being called somewhere in
ActiveRecord::Base in
order to effect that.
But I would then think that I should be able to say
include ActiveRecord::Validations::ClassMethods
in my class definition in order to get the validation helpers accessible
at the instance level. When I do that I get the following error when
validate is called:
SystemStackError: stack level too deep
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
`validates_presence_of''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:402:in
`each''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:402:in
`validates_presence_of''
from script/../config/../config/../app/models/web_user.rb:62:in
`validate''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
`send''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
`validates_presence_of''
... 6446 levels...
Some sort of recursion is happening, but I''m at the end of my Ruby
abilities. Any help would be much appreciated. I''m trying to be DRY (or
at least not repeat others), but if I can''t figure this out soon
I''ll
just have to rewrite the validations I need manually.
Thanks!
/afb
--
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
-~----------~----~----~----~------~----~------~--~---
Adam Block wrote:> Is it possible to use the built-in validation helper methods from within > my own validate() (or validate_save() or validate_create()) method, or > are they limited to only being used as class methods? I need to override > validate() but it would still be helpful to take advantage of the > existing helpers. > > Thanks! > > /afbvalidate is called in addition to any class_method defined validations, not instead of. The following if perfectly valid: class Foo < ActiveRecord::Base validates_presence_of :bar validates_numercality_of :some_integer def validate if !for_sale && price.nil? errors.add :price, "foo''s that are for sale must have a price!" end end 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 -~----------~----~----~----~------~----~------~--~---
Thanks. Yeah, I''ve probably been thinking about this wrong. I need to
do
a bunch of conditional validation. I know I can write a method to test
the conditions and pass that to the helpers in the :if parameter. It
just seemed cleaner to override validate(), but that would require a
bunch of extra code to do what I would otherwise do with the helpers. So
probably not in fact cleaner.
One thing I haven''t seen. I presume this is okay:
validates_presence_of :name, :address, :if :test1
validates_presence_of :name, :address, :phone, :if :test2
Is that the accepted way to implement that kind of conditional
validation?
/afb
--
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
-~----------~----~----~----~------~----~------~--~---
Adam Block wrote:> Thanks. Yeah, I''ve probably been thinking about this wrong. I need to do > a bunch of conditional validation. I know I can write a method to test > the conditions and pass that to the helpers in the :if parameter. It > just seemed cleaner to override validate(), but that would require a > bunch of extra code to do what I would otherwise do with the helpers. So > probably not in fact cleaner. > > One thing I haven''t seen. I presume this is okay: > > validates_presence_of :name, :address, :if :test1 > validates_presence_of :name, :address, :phone, :if :test2 > > Is that the accepted way to implement that kind of conditional > validation? > > /afbmore like: validates_presence_of :name, :address, :if => :foo? def foo? foo == ''bar'' end pretty sure that works. if it doesnt validates_presence_of :name, :address, :if => Proc.new { foo? } -- 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 -~----------~----~----~----~------~----~------~--~---