Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for 'after_save_commit' [ci skip] #35861

Merged
merged 1 commit into from Apr 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions guides/source/active_record_callbacks.md
Expand Up @@ -473,10 +473,33 @@ end
=> User was saved to database
```

To register callbacks for both create and update actions, use `after_commit` instead.
There is also an alias for using the `after_commit` callback for both create and update together:

* `after_save_commit`

```ruby
class User < ApplicationRecord
after_save_commit :log_user_saved_to_db

private
def log_user_saved_to_db
puts 'User was saved to database'
end
end

# creating a User
>> @user = User.create
=> User was saved to database

# updating @user
>> @user.save
=> User was saved to database
```

To register callbacks for both create and destroy actions, use `after_commit` instead.

```ruby
class User < ApplicationRecord
after_commit :log_user_saved_to_db, on: [:create, :update]
after_commit :log_user_saved_to_db, on: [:create, :destroy]
end
```