Jorge Mario G. Mazo
2007-Mar-09 03:45 UTC
validates_presence_of with validates_each dont work together
Hi there
how do I make validates_presence_of and validates each work?
here is my code
--------
#validates_presence_of :quantity
#validates_numericality_of :quantity
validates_each :quantity do |record, attr, value|
if value < User.find(:first, :conditions => [''id =
?'',
record.user_id]).minimumbottles
@min = User.find(:first, :conditions => [''id = ?'',
record.user_id]).minimumbottles
record.errors.add attr, ''=> '' + @min.to_s +
'' is your minimum
order quantity for
the current program, please contact admin to change program
details''
end
end
----
but if I use validates_each validates_presence_of doesnot work
and I would like to use both
any help with very appreciated
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Jorge Mario G. Mazo
2007-Mar-09 03:54 UTC
Re: validates_presence_of with validates_each dont work together
I forgot to mention I saw the :allow_nil option in the api docs but I dont know how to use it in my case On 3/8/07, Jorge Mario G. Mazo <vadersolo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there > how do I make validates_presence_of and validates each work? > here is my code > -------- > #validates_presence_of :quantity > #validates_numericality_of :quantity > > validates_each :quantity do |record, attr, value| > if value < User.find(:first, :conditions => [''id = ?'', > record.user_id]).minimumbottles > @min = User.find(:first, :conditions => [''id = ?'', > record.user_id]).minimumbottles > record.errors.add attr, ''=> '' + @min.to_s + '' is your minimum > order quantity for > the current program, please contact admin to change program details'' > end > end > ---- > but if I use validates_each validates_presence_of doesnot work > and I would like to use both > any help with very appreciated >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
James Newport
2008-May-02 00:34 UTC
Re: validates_presence_of with validates_each dont work toge
You could add an if statement to the top of your validates_each block as
such to emulate what validates_presence_of does eg:
validates_each :quantity do |model, attr, value|
if value.blank?
model.errors.add(attr, "can''t be blank")
elsif value < User.find(:first, :conditions => [''id =
?'',
record.user_id]).minimumbottles
...
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
-~----------~----~----~----~------~----~------~--~---