I have a model where an age range is specified, and I need to ensure that if a maximum age is specified, the minimum age is less than or equal to this age. I attempted to setup the validation like so: validates_inclusion_of :min_age, :in=> 0..:max_age, :message => ''min_age_greater_than_max_age'', :if => :max_age_set? Where #max_age_set? returns true if the maximum age is set. However, I am receiving an Argument Error - An object with the method include? is required must be supplied as the :in option of the configuration hash. This indicates that the range specified is not actually a range. Is there another way I should be accessing this attribute with the validation helpers? Thanks in advance, Derek -- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
Rodrigo Kochenburger
2005-Oct-05 18:37 UTC
Re: validates_inclusion_of between 2 attributes
Derek, Try this. class YourModel < ActiveRecord::Base attr_writer :min_age_range after_initialize :init validate :validate_range private def init @min_age_range = 0..18 #default range end def validate_range @min_age_range.include?(self.min_age) end end I didn''t test it, but it might work. Then you can do something like this inside the controller: obj = YourModel.find(id) obj.min_age_range = 18..90 # more stuff here obj.save Rodrigo Kochenburger Date: Wed, 5 Oct 2005 11:15:35 -0400> From: Derek Haynes <derek.haynes-cRsPOODq9VkXQ3Lr6voeyA@public.gmane.org> > Subject: [Rails] validates_inclusion_of between 2 attributes > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Message-ID: > <8eee6840510050815m1df1ea07v2b89666f41f8c12d-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> > Content-Type: text/plain; charset=ISO-8859-1 > > I have a model where an age range is specified, and I need to ensure > that if a maximum age is specified, the minimum age is less than or > equal to this age. > > I attempted to setup the validation like so: > > validates_inclusion_of :min_age, :in=> 0..:max_age, :message => > ''min_age_greater_than_max_age'', :if => :max_age_set? > > Where #max_age_set? returns true if the maximum age is set. > > However, I am receiving an Argument Error - An object with the method > include? is required must be supplied as the :in option of the > configuration hash. > > This indicates that the range specified is not actually a range. Is > there another way I should be accessing this attribute with the > validation helpers? > > Thanks in advance, > > Derek >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails