I''m not sure what''s the difference between before_validation an before_validation_on_create. Is it that before_validation gets invoked before a create and update call, whereas the latter just gets invoked on create call? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Yes, exactly. before_validation happens before every validation. That means: model.valid? model.save model.create before_validation_on_create only happens when saving a new record. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QTN9JUswWM4J. For more options, visit https://groups.google.com/groups/opt_out.
Andrew Vit wrote in post #1071395:> Yes, exactly. before_validation happens before every validation. That > means: > > model.valid? > model.save > model.create > > before_validation_on_create only happens when saving a new record.How do these before validation callbacks work? Are they just simple function callbacks, or can you return false or errors.add in them to halt the rest of the record.save code? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 6 August 2012 14:30, masta Blasta <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Andrew Vit wrote in post #1071395: >> Yes, exactly. before_validation happens before every validation. That >> means: >> >> model.valid? >> model.save >> model.create >> >> before_validation_on_create only happens when saving a new record. > > How do these before validation callbacks work? Are they just simple > function callbacks, or can you return false or errors.add in them to > halt the rest of the record.save code?Have a look at the Rails Guide on ActiveRecord Validations and Callbacks. That may answer your questions. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
masta Blasta
2012-Aug-06 18:02 UTC
Re: Re: before_validation an before_validation_on_create
Colin Law wrote in post #1071428:> On 6 August 2012 14:30, masta Blasta <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> How do these before validation callbacks work? Are they just simple >> function callbacks, or can you return false or errors.add in them to >> halt the rest of the record.save code? > > Have a look at the Rails Guide on ActiveRecord Validations and > Callbacks. That may answer your questions. > > ColinThanks for the pointer. Here''s the quick answer for future users, from RoR api: "If the returning value of a before_validation callback can be evaluated to false, the process will be aborted and Base#save will return false. If ActiveRecord::Validations#save! is called it will raise a ActiveRecord::RecordInvalid exception. Nothing will be appended to the errors object. ... If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last." -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.