# Multi Row validation... or running queries within Validate f

**URL:** <https://www.ruby-forum.com/t/multi-row-validation-or-running-queries-within-validate-f/52325>\
**Category:** Rails\
**Created:** [January 12, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/multi-row-validation-or-running-queries-within-validate-f/52325 "2006-01-12T21:33:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [January 12, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/multi-row-validation-or-running-queries-within-validate-f/52325/1 "2006-01-12T21:33:35Z")

</div>

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.
