Mark Farver
2006-Jan-12 20:32 UTC
[Rails] Multi Row validation... or running queries within Validate function?
Is there anyway to perform a query from within a validate function? I need to verify that the sum of several rows does not exceed a certain value eg: class Entry < ActiveRecord::Base belongs_to :day belongs_to :project def validate totalhours = hours samedayentries = self.find_by_day_id(day_id) samedayentries.each do |i| totalhours += i[:hours] unless (id == i[:id]) end errors.add_to_base( "You cannot enter more than 24 hours in a day.") if totalhours > 24 end end Or some better way to accomplish the same general idea. (I can do it in the controller, but it really belongs in the model IMHO.) Thanks Mark Farver