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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sun, Jul 27, 2008 at 10:47 PM, Scott Kulik <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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> -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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#M001045thanks. 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---