Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Returning false in around_* callback still firing after_* callbacks? #7127

Closed
jingoro opened this Issue · 6 comments

5 participants

@jingoro

When returning false in an around_* callback, this will halt the chain and prevent the method from being executed. However, after_* callbacks are still being triggered. This behavior does not occur with before_*.

Here is some example code:

require 'active_model'

class A
  extend ActiveModel::Callbacks
  define_model_callbacks :create
  def create; run_callbacks(:create) { p 'create A' }; end
  before_create { false }
  after_create { p 'not here' }
end

class B
  extend ActiveModel::Callbacks
  define_model_callbacks :create
  def create; run_callbacks(:create) { p 'create B' }; end
  around_create { false }
  after_create { p 'but here?' }
end

a = A.new; p a.create
b = B.new; p b.create

This results in:

false
"but here?"
nil

This was tested on both ActiveModel 3.2.6 and ActiveModel on master. Is this a bug or expected behavior?

See mongoid/mongoid#2198

@mjio

I'm not quite sure if this is an expected behavior. I only can find a single test, actually saying that the after callback shouldn't be executed:

https://github.com/rails/rails/blob/master/activemodel/test/cases/callbacks_test.rb#L60

@jingoro

My inclination is that this is a bug for a couple reasons:

  1. If you return false in a before callback, it stops execution (of save) and does not execute any after callbacks.
  2. Returning false in the around callback before yield does stop execution (of the save), and causes save to return to false. Thus, it would seem more natural that it would follow the same pattern of a before callback and not execute any after callbacks.
@kennyj
Collaborator

@jingoro I sent the above PR. Please confirm it.

@jingoro

@kennyj Just left a note on #8479 confirming the PR.

@jingoro jingoro added the stale label
@rafaelfranca
Owner

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rafaelfranca rafaelfranca added attached PR and removed stale labels
@rafaelfranca rafaelfranca self-assigned this
@sgrif
Collaborator
@sgrif sgrif closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.