AR around callbacks defined with block are broken #7344

Closed
rwz opened this Issue Aug 13, 2012 · 1 comment

Comments

Projects
None yet
2 participants
Contributor

rwz commented Aug 13, 2012

class Entry < AR::Base
  around_update do
    puts 'foo'
    yield
    puts 'bar'
  end
end

on actual update:

LocalJumpError: no block given (yield)
  from /my-app-path/app/models/entry.rb:4:in `block in <class:Entry>'
  from /rubygems-path/activesupport-3.2.8/lib/active_support/callbacks.rb:214:in `_conditional_callback_around_33'

The callback defined with method works fine though.

class Entry < AR::Base
  def _my_around_update_callback
    puts 'foo'
    yield
    puts 'bar'
  end

  around_update :_my_around_update_callback
end

You have to add the object and block arguments when using the block version, and call the block instead of yielding, like this:

class Entry < ActiveRecord::Base
  # Since you're not on the instance, you need the record here.
  # And also have to call the block "manually".
  around_update do |record, block|
    puts 'foo'
    block.call
    puts 'bar'
  end

  around_update :with_method
  # Here you have an instance method, so no record 
  # and also no need for manual call, just use yield.
  def with_method
    puts 'foo method'
    yield
    puts 'bar method'
  end
end

## Which would give you an output like this:

>> Entry.first.save
foo
foo method
bar method
bar

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment