Does anyone how to create a validation that: validates_presence_of :a OR :b I.e. :a OR :b must be present. I thought this would probably be possible with some kind of validation callback, e.g.: def check_a_or_b_is_set return (a or b) end Any ideas? Thanks, Rob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/02eeb48a/attachment.html
How about something like: validates_each :a do |record, attribute, value| if value.blank? and record.send(:b).blank? record.errors.add_to_base ''a or b required'' end end On 01/04/06, Robert MannI <robmnl@gmail.com> wrote:> Does anyone how to create a validation that: > > validates_presence_of :a OR :b > > > I.e. :a OR :b must be present. > > I thought this would probably be possible with some kind of validation > callback, e.g.: > > def check_a_or_b_is_set > return (a or b) > end > > > > Any ideas? > > > Thanks, > Rob > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Another way to do ''custom'' validation is to simply supply a "validate" method in your class. This gets called before you save/update. You can simplify a bit what Ian has suggested. #just place this in your model class def validate errors.add_to_base "A or B are required" if a.blank? and b.blank? end Hope this helps! -Nick
You could do something like: validates_presence_of :a, :if => Proc.new { |r| !r.b? } validates_presence_of :b, :if => Proc.new { |r| !r.a? } -Jonny. On 4/2/06, Robert MannI <robmnl@gmail.com> wrote:> > Does anyone how to create a validation that: > > validates_presence_of :a OR :b > > > I.e. :a OR :b must be present. > > I thought this would probably be possible with some kind of validation > callback, e.g.: > > def check_a_or_b_is_set > return (a or b) > end > > > > Any ideas? > > > Thanks, > Rob > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060402/def8345b/attachment.html
Thanks alot for all three suggestions! The "validate" callback was what I was looking for, as it allows me to make the validations as complex as I like. Thanks! Rob On 4/2/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:> > You could do something like: > > validates_presence_of :a, :if => Proc.new { |r| !r.b? } > validates_presence_of :b, :if => Proc.new { |r| !r.a? } > > -Jonny. > > On 4/2/06, Robert MannI <robmnl@gmail.com> wrote: > > > Does anyone how to create a validation that: > > validates_presence_of :a OR :b > > > I.e. :a OR :b must be present. > > I thought this would probably be possible with some kind of validation > callback, e.g.: > > def check_a_or_b_is_set > return (a or b) > end > > > > Any ideas? > > > Thanks, > Rob > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060402/bc2ce948/attachment.html