Forum: Ruby on Rails Multi Row validation... or running queries within Validate f

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mark F. (Guest)
on 2006-01-12 22:33
(Received via mailing list)
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 ==
                errors.add_to_base( "You cannot enter more than 24 hours
in a day.") if totalhours > 24

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.)

Mark F.
This topic is locked and can not be replied to.