I''m playing around with ActiveScaffold and noticed my validations are being called twice. I''m really not sure if this is a Rails problem or something caused by ActiveScaffold, but is it normal that all the validations callbacks are being called twice on create/update. Take a simple model: class Company < ActiveRecord::Base def validate_on_create puts "ON CREATE" end def validate puts "MAIN VALIDATE" end end I am seeing the following in the console upon create: MAIN VALIDATE ON CREATE MAIN VALIDATE ON CREATE This is from a fresh install of InstantRails 1.7 with the latest ActiveScaffold. -- 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 -~----------~----~----~----~------~----~------~--~---
Nic Geadah wrote:> I''m playing around with ActiveScaffold and noticed my validations are > being called twice.Ideally, there should be only two kinds of methods in the world: - queries?, which return a value without side-effects - commands!, which only have side-effects In real life, some methods create both side-effects and return values. However, we should strictly treat validations as the first kind. And all queries, such as valid?, are free to be called as many times as is convenient. The scaffold probably has the equivalent of valid? and save! for whatever reason. Pay it no mind, and write your validations without side-effects. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks - and you are perfectly right about side-effects from validation methods. My problem is not with undesirable side effects from the validation method, but rather a performance concern. I validate the contents of an uploaded file (which could be large) and I''d rather avoid doing so much I/O twice. I guess I''m stuck creating a hack in my validations to bypass it the "second time around". -- 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 -~----------~----~----~----~------~----~------~--~---
Nic Geadah wrote:> Thanks - and you are perfectly right about side-effects from validation > methods. My problem is not with undesirable side effects from the > validation method, but rather a performance concern. I validate the > contents of an uploaded file (which could be large) and I''d rather avoid > doing so much I/O twice. I guess I''m stuck creating a hack in my > validations to bypass it the "second time around".Then that is absolutely not a "hack" - it is enforcing the contract of the query? interface. Have you timed the process to see how long it takes? And can you write unit tests that cover the situation? I would worry the data would get dirty and the validation would return its obsolete value. That''s well over the threshold that I would test for. --~--~---------~--~----~------------~-------~--~----~ 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 Jun 17, 2:11 am, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The scaffold probably has the equivalent of > > valid? and save! > > for whatever reason. Pay it no mind, and write your validations without > side-effects.If you''ve actually got validations like validates_uniqueness_of etc... then they will get run multiple times if your model is loaded multiple times Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---