On Jun 16, 2005, at 11:39 PM, Matt Jankowski wrote:
> I think that "dynamic validation" kinda describes what
I''m trying to
> do.
>
> I have a Score class, which belongs_to :rating
>
> Rating has min and max methods which return the correct values for a
> particular type of rating. (these are usually 0/10, 0/100, etc)
>
> Essentially, what I want Score to validate as, conceptually, is this...
>
> class Score < ActiveRecord::Base
> belongs_to :rating
> validates_inclusion_of :score, :in =>
> self.rating.min..self.rating.max
> end
>
> That way, for any particular score, if someone attempts to enter say
> "50" for a score whose rating only wants 0..10 - I can not save
that
> score.
>
> Does that make sense? Is there a way to do what I want w/
> validations, or is there a more correct place for it?
>
> -Matt
You can always define your own validate method [1] and do anything the
validation macros won''t do in there. Like:
def validate
errors.add("score", "no good") unless
(self.rating.min..self.rating.max).include?(self.score)
end
And so on. Back in the early days we were walking to school up hill
both ways and writing our own validate methods! You''ve got it too easy
these days with your newfangled convenience macros ;)
[1] http://rails.rubyonrails.com/classes/ActiveRecord/Validations.html
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails