I''m fairly new to Rails so I''m having some trouble with what is probably an easy task. Any assistance would be appreciated. I have two fields, I only have to have one of them filled in... At the moment I''m working on some variation of validates_presence_of :foo, :if => "bar.empty?" Verbatim, that will of course not work.. Suggestions? -- 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 -~----------~----~----~----~------~----~------~--~---
On 16-Mar-07, at 4:14 PM, Tom von S. wrote:> > I''m fairly new to Rails so I''m having some trouble with what is > probably > an easy task. Any assistance would be appreciated. > > I have two fields, I only have to have one of them filled in... > > At the moment I''m working on some variation of > > validates_presence_of :foo, :if => "bar.empty?" > > Verbatim, that will of course not work.. > > Suggestions?Tom, :if takes a ''block'' validates_presence_of :foo, :if => Proc.new { | your_model_instance | your_model_instance.bar.empty? } as you can see, the block is passed the current instance, which you can examine - returning tru means the validation will take place. cheers, Jodi --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jodi Showers wrote:> Tom, :if takes a ''block'' > > validates_presence_of :foo, :if => Proc.new { | your_model_instance > | your_model_instance.bar.empty? } > > as you can see, the block is passed the current instance, which you > can examine - returning tru means the validation will take place.Ah, much like the example in the docs (which I somehow didn''t "get"). Thank you, Tom -- 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 -~----------~----~----~----~------~----~------~--~---