There's a small inconsistency in ActiveRecord's callback syntax that has 
tripped me up before. It wouldn't be a big deal, but it can lead to a 
silent failure. I'd like to suggest that it either be made consistent or be 
made to fail loudly.

The issue is that to do something before validating, but only when 
creating, you use `before_validation on: :create`, but to do something 
before saving, but only when creating, you use `before_create`. 

I have tried to use `before_save on: :create`, and it does not produce an 
error, but it does something unexpected: it saves before create **and** 
before update. This isn't really something an application's tests would 
catch, I don't think.

I propose one of the following changes:

1. `before_save on: :create` and `before_save on: :update` could be 
supported (and the same for `after_save` and `after_commit`)
2. Using `before_save on: :create` and the like could raise an exception 
saying either "this isn't supported" or "this isn't supported but will be 
eventually"
3. The `before_validation on: :create` syntax could be dropped in favor of 
`before_save_validation` and `before_create_validation`, so that all a 
user's hook declarations are either successful or get `NoMethodError`

Thoughts?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-core/-/CU3L9lEhvTkJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to