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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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 ==
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 F.
This topic is locked and can not be replied to.