Hi everyone, I just need a quick help :) What is the best way to write some code in :presence option? I have a Question model and one of the fields is question_type: - example question - static question Each question has alternatives, and each of them has scores What I want is to validate the presence of :score, but when its only on the static question. I dont want to validate the presence of the score in examples validates :score, :presence => {:if => ???? } What should I do? Thanks Javier Q -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Well I found the code here: http://stackoverflow.com/questions/1673812/rails-validation-for-users-email-only-want-it-to-validate-when-a-user-signs-up validates :score, :presence => true, :if => Proc.new{ (some code here) } -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Dec 14, 2011 at 12:41 AM, JavierQQ <jquarites-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well I found the code here: > > > http://stackoverflow.com/questions/1673812/rails-validation-for-users-email-only-want-it-to-validate-when-a-user-signs-up > > validates :score, :presence => true, :if => Proc.new{ (some code > here) } > >while this may work in your case, you should remember the following (a shameless copy and paste from the api) Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given to one specific validator, as a hash: validates :password, :presence => { :if => :password_required? }, :confirmation => true Or to all at the same time: validates :password, :presence => true, :confirmation => true, :if => :password_required? one more thing. I find it cleaner to do what you need like this validates :score, :presence => {:if => :static?} def static? #code here end def example? #code here end Cheers!> -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Dec 13, 2011 at 8:16 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > while this may work in your case, you should remember the following (a > shameless copy > and paste from the api) > > Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can > be given to one specific validator, as a hash: > > validates :password, :presence => { :if => :password_required? }, :confirmation => true > > Or to all at the same time: > > validates :password, :presence => true, :confirmation => true, :if => :password_required? > > > one more thing. I find it cleaner to do what you need like this > > validates :score, :presence => {:if => :static?} > > def static? > #code here > end > > def example? > #code here > end > > Cheers! >Thank! that''s what I found on api, but I wasnt sure if it was the best way of doing that (and by that I mean the easier way to do that "filter") I''ll try what you mention because I wasn''t sure enough what":password_required?" mean ( I tought it was a sort of helper... :) ) Javier Q -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.