What is the difference between before_create :foo and before_save :foo, on:
:create?

I think this is more a documentation issue and maybe (2.) can help to avoid
confusion.

I don't like (1.) and (3.).

Rafael Mendonça França
http://twitter.com/rafaelfranca
https://github.com/rafaelfranca



On Tue, Nov 13, 2012 at 12:24 AM, Allam Marcos Campanini Matsubara <
allam.matsub...@gmail.com> wrote:

> +1 for alternative 1, "`before_save on: :create` and `before_save on:
> :update` could be supported (and the same for `after_save` and
> `after_commit`)".
>
>
> On 10 November 2012 11:51, Nathan Long <nathanml...@gmail.com> wrote:
>
>> 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 rubyonrails-core@googlegroups.com.
>> To unsubscribe from this group, send email to
>> rubyonrails-core+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/rubyonrails-core?hl=en.
>>
>
>
>
> --
> Allam Marcos Campanini Matsubara
>
> skype: allam.matsubara
> twitter: @allam_matsubara
> Phone: +55 (41) 8847-8677
>
>
>  --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> 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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to