Jarred Sumner
2012-May-26 23:04 UTC
before_create is after_validatation :on => :create , when it should be before_validation :on => :create
In ActiveRecord''s callbacks, methods within a before_create callback are called after the validation, but before the model is created. This was an intentional design decision, but I think it was the wrong one. What do you think? When I think of before_create, I think of before_validation :foo, :on => :create, but, by default, before_create is synonymous with after_validation :foo, :on => :create`, which is technically correct, but confusing. Most of the goal of convention over configuration is to have sane defaults, right? Wouldn''t following that principle include favoring what makes the most sense at first thought, over a technicality? And, I''m calling this a technicality as someone who didn''t immediately realize that before_create is invoked after validations. It makes logical sense, but this is an issue that, most likely, many inexperienced developers will/do encounter. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Rodrigo Rosenfeld Rosas
2012-May-27 00:33 UTC
Re: before_create is after_validatation :on => :create , when it should be before_validation :on => :create
It makes sense to me. I expect before_create to run after validation, as should be only called if the object is about to be created. That means it has already passed validation. Having said that, I usually only use before_validation hooks and I can''t remember the last time I used before_creation, if I ever used... Em 26-05-2012 20:04, Jarred Sumner escreveu:> In ActiveRecord''s callbacks, methods within a before_create callback > are called after the validation, but before the model is created. > > This was an intentional design decision, but I think it was the wrong > one. What do you think? > > When I think of before_create, I think of before_validation :foo, :on > => :create, but, by default, before_create is synonymous with > after_validation :foo, :on => :create`, which is technically correct, > but confusing. Most of the goal of convention over configuration is to > have sane defaults, right? Wouldn''t following that principle include > favoring what makes the most sense at first thought, over a > technicality? > > And, I''m calling this a technicality as someone who didn''t immediately > realize that before_create is invoked after validations. It makes > logical sense, but this is an issue that, most likely, many > inexperienced developers will/do encounter. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Pedro Nascimento
2012-May-27 03:38 UTC
Re: before_create is after_validatation :on => :create , when it should be before_validation :on => :create
I use before_create often and I do find it reasonable to be after_validation. If you''re ever in doubt, Rails Guides explains the callback order before anything else: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html On Sat, May 26, 2012 at 9:33 PM, Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com> wrote:> It makes sense to me. I expect before_create to run after validation, as > should be only called if the object is about to be created. That means it > has already passed validation. > > Having said that, I usually only use before_validation hooks and I can''t > remember the last time I used before_creation, if I ever used... > > Em 26-05-2012 20:04, Jarred Sumner escreveu: > > In ActiveRecord''s callbacks, methods within a before_create callback >> are called after the validation, but before the model is created. >> >> This was an intentional design decision, but I think it was the wrong >> one. What do you think? >> >> When I think of before_create, I think of before_validation :foo, :on >> => :create, but, by default, before_create is synonymous with >> after_validation :foo, :on => :create`, which is technically correct, >> but confusing. Most of the goal of convention over configuration is to >> have sane defaults, right? Wouldn''t following that principle include >> favoring what makes the most sense at first thought, over a >> technicality? >> >> And, I''m calling this a technicality as someone who didn''t immediately >> realize that before_create is invoked after validations. It makes >> logical sense, but this is an issue that, most likely, many >> inexperienced developers will/do encounter. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.**com<rubyonrails-core@googlegroups.com> > . > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@**googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at http://groups.google.com/** > group/rubyonrails-core?hl=en<http://groups.google.com/group/rubyonrails-core?hl=en> > . > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.