I have an object called dealer. Dealer has two fields dealer_number
and sub_dealer_number. It must have 1 OR the other... but not both.
To validate this I have:
class Dealer < ActiveRecord::Base
validate :dealer_id?
private
def dealer_id?
if number.blank? && sub_number.blank?
errors.add_to_base("You must specify either a Dealer Number OR a
Sub Dealer Number.")
elsif (number.length > 0) && (sub_number.length > 0)
errors.add_to_base("You must specify either a Dealer Number OR
a Sub Dealer Number.")
end
end
end
This looks really messy to me. Is there a better way? Thank you for
your thoughts.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Oct-22 05:48 UTC
Re: Cleaner way to build a "one or the other" validation?
On 21 Oct 2008, at 12:58, gnunix wrote:> > I have an object called dealer. Dealer has two fields dealer_number > and sub_dealer_number. It must have 1 OR the other... but not both. > To validate this I have: >Use the Xor operator ( ^ ) ? Fred> class Dealer < ActiveRecord::Base > validate :dealer_id? > > private > def dealer_id? > if number.blank? && sub_number.blank? > errors.add_to_base("You must specify either a Dealer Number OR a > Sub Dealer Number.") > elsif (number.length > 0) && (sub_number.length > 0) > errors.add_to_base("You must specify either a Dealer Number OR > a Sub Dealer Number.") > end > end > end > > This looks really messy to me. Is there a better way? Thank you for > your thoughts. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pardee, Roy
2008-Oct-22 15:14 UTC
Re: Cleaner way to build a "one or the other" validation?
How about:
def validate_dealer_id
if number.blank? then
errors.add_to_base("You must specify either a Dealer Number OR a Sub
Dealer Number.") if subnumber.blank?
else
erros.add_to_base("A dealer can''t have both a dealer number
AND a sub dealer number") unless subnumber.blank?
end
end
?
Extra bonus kibbitzing:
- Would it make things any easier to store both those
numbers in a single field, and keep track of whether
a given dealer is a dealer or a sub in a separate
''dealer_type''
field?
- FWIW--the ruby convention is that methods whose names
end in a ? return a boolean.
Cheers,
-Roy
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk@googlegroups.com] On Behalf Of gnunix
Sent: Tuesday, October 21, 2008 9:58 AM
To: Ruby on Rails: Talk
Subject: [Rails] Cleaner way to build a "one or the other" validation?
I have an object called dealer. Dealer has two fields dealer_number and
sub_dealer_number. It must have 1 OR the other... but not both.
To validate this I have:
class Dealer < ActiveRecord::Base
validate :dealer_id?
private
def dealer_id?
if number.blank? && sub_number.blank?
errors.add_to_base("You must specify either a Dealer Number OR a Sub
Dealer Number.")
elsif (number.length > 0) && (sub_number.length > 0)
errors.add_to_base("You must specify either a Dealer Number OR a
Sub Dealer Number.")
end
end
end
This looks really messy to me. Is there a better way? Thank you for your
thoughts.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Michael Kahle
2008-Oct-22 21:41 UTC
Re: Cleaner way to build a "one or the other" validation?
Frederick Cheung wrote:> Use the Xor operator ( ^ ) ? > > FredSexy. Thanks. I changed above to: class Dealer < ActiveRecord::Base validate :dealer_id? private def dealer_id? if !(number.blank? ^ sub_number.blank?) errors.add_to_base("You must specify either a Dealer Number OR a Sub Dealer Number.") end end end Makes me feel less dirty. -- 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 -~----------~----~----~----~------~----~------~--~---
Michael Kahle
2008-Oct-22 21:43 UTC
Re: Cleaner way to build a "one or the other" validation?
Roy Pardee wrote:> How about: > > def validate_dealer_id > if number.blank? then > errors.add_to_base("You must specify either a Dealer Number OR a Sub > Dealer Number.") if subnumber.blank? > else > erros.add_to_base("A dealer can''t have both a dealer number AND a > sub dealer number") unless subnumber.blank? > end > end > > ? > > Extra bonus kibbitzing: > - Would it make things any easier to store both those > numbers in a single field, and keep track of whether > a given dealer is a dealer or a sub in a separate ''dealer_type'' > field? > - FWIW--the ruby convention is that methods whose names > end in a ? return a boolean. > > Cheers, > > -RoyI don''t think that your above example is any cleaner then my original solution. However, your bonus kibbitzing (lol) I think is a really good thought. I am going to implement this logic on a later iteration. Thanks for this. -- 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2008-Oct-23 02:19 UTC
Re: Cleaner way to build a "one or the other" validation?
Michael Kahle wrote:> I don''t think that your above example is any cleaner then my original > solution. However, your bonus kibbitzing (lol) I think is a really good > thought. I am going to implement this logic on a later iteration. > Thanks for this.Hi Michael, For future reference, I needed to do something similar a few months ago and ended up creating a custom validation. I wrote about it if you want to look into it: http://per-snicket-y.blogspot.com/2008/04/custom-validations-in-rails-202.html Peace. -- 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 -~----------~----~----~----~------~----~------~--~---