On 5 March 2010 17:02, xxdesmus <xxdes...@gmail.com> wrote: > So I have a bunch of form fields that I need to add up their > values, and make sure their sum does not except the value entered in a > new form field. If the sum does exceed the form field value then I > want to alert the user and prevent it from saving. Basically I want to > validate that :atWorkHours.value > ( :hoursMeetings.value > + :hoursTraining.value + :hoursProjTrav.value ) > > I tried to do something similar to this in my model...but I assume I > just had the syntax completely wrong.
Assuming all of the "hours" attributes all return numbers (something, somewhere is ensuring that, I hope ;-) -- you can add some validations to your model. Again, there's a couple of ways of doing this - I normally separate the method that adds the error to base from the code that checks the condition (so I can check the condition myself if I want), but you could easily merge them together if you prefer: validate :validate_work_hours_not_exceeded def work_hours_exceeded? # this is a little different to your equation, as yours would raise an error if both sides were exactly the same, and I'd guess that's not the right result - but if not, adjust it to suit [hours_at_meetings, hours_in_training, hours_travelling].sum > hours_at_work end private def validate_work_hours_not_exceeded errors.add_to_base("Do not exceed working hours") if work_hours_exceeded? end I took the liberty of tweaking the variable names (although you were showing them as symbols...) to make them a little more in line with conventions (and, I think, more easily readable - YMMV :-) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.