Can I use validates_presence_of somehow if I want to check that at least one of 2 fields is not empty? Thanks, Dmitry -- 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 -~----------~----~----~----~------~----~------~--~---
I''m still new to Rails, but I think you need to add a ''validate'' method to your controller and perform the check in there instead. Off the top of my head, something like: protected def validate errors.add("Either tweedledum or tweedledee need to be entered.") unless tweedledum != nil or tweedledee != nil end I can''t see a way of using validates_presence_of for this. Regards, Chris. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Dew wrote:> I''m still new to Rails, but I think you need to add a ''validate'' method > to your controller and perform the check in there instead. > > Off the top of my head, something like: > > protected > def validate > errors.add("Either tweedledum or tweedledee need to be entered.") > unless tweedledum != nil or tweedledee != nil > end > > I can''t see a way of using validates_presence_of for this. > > Regards, > > Chris.Thanks, Chris! And is it possible to embed this ''validate'' method into the model; say, into the before_save filter or something like that? -- 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 -~----------~----~----~----~------~----~------~--~---
I think Chris meant to tell you to add the validate method to the model. This will then be called in the normal validation routine. Mark On 9/7/06, Dmitry Hazin <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Chris Dew wrote: > > I''m still new to Rails, but I think you need to add a ''validate'' method > > to your controller and perform the check in there instead. > > > > Off the top of my head, something like: > > > > protected > > def validate > > errors.add("Either tweedledum or tweedledee need to be entered.") > > unless tweedledum != nil or tweedledee != nil > > end > > > > I can''t see a way of using validates_presence_of for this. > > > > Regards, > > > > Chris. > > Thanks, Chris! > And is it possible to embed this ''validate'' method into the model; say, > into the before_save filter or something like that? > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Mark Van Holstyn mvette13-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://lotswholetime.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 -~----------~----~----~----~------~----~------~--~---
> Thanks, Chris! > And is it possible to embed this ''validate'' method into the model; say, > into the before_save filter or something like that? >If you have a method called "validate" on your model, this is used automatically by the valid? method. No need to do anything else. Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi ! 2006/9/7, Max Muermann <ruby@muermann.org>:> If you have a method called "validate" on your model, this is used > automatically by the valid? method. No need to do anything else.The only problem with that is if you have subclasses that override the validate method. Unless the subclass calls the superclass method, your validations won't work as expected. class Order < AR::Base def validate # this will never be called end end class RushOrder < Order def validate self.errors.add(:status, 'invalid') if self.status.blank? end end If you do this instead: class Order < AR::Base before_validation :check_base_order def check_base_order # this will always be called end end class RushOrder < Order before_validation :check_rush_order def check_rush_order # this will ALSO always be called end end Hope that helps ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Yes, I meant model, but wrote controller. Also it should be @tweedledum etc. The ''unless'' is a continuation of the ''errors.add'' line, but it''s come out with bad formatting. Regards, Chris. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---