11175
(-- --)
July 28, 2008, 3:47am
1
i'm trying to use a custom validation but for some reason it's seems
like it is just getting ignored.
in my model:
before_save :validate
def validate
total_rank = 0
for compound_material in self.compound_materials
total_rank += self.item.rank
end
avg_rank = total_rank / self.compound_materials.length
max_rank = avg_rank + 4
if self.item.rank > max_rank
errors.add_to_base "The compound entered is not valid because it
is greater than rank " + self.item.rank
end
end
anyone know what i should be doing differently?
i'm trying to use a custom validation but for some reason it's seems
like it is just getting ignored.
in my model:
before_save :validate
def validate
total_rank = 0
for compound_material in self.compound_materials
total_rank += self.item.rank
end
avg_rank = total_rank / self.compound_materials.length
max_rank = avg_rank + 4
if self.item.rank > max_rank
errors.add_to_base "The compound entered is not valid because it
is greater than rank " + self.item.rank
end
end
anyone know what i should be doing differently?
I believe that you need to return false from the filter in order to
halt the save process.
A better solution would be to use validates_each
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001045
11175
(-- --)
July 28, 2008, 11:25pm
3
I believe that you need to return false from the filter in order to
halt the save process.
A better solution would be to use validates_each
Peak Obsession
thanks. returning false did the trick. I'm not sure if validate each
will work in this case because I am not actually validating a field for
this object. I was validating compound.item.rank.