Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:14:17+00:00 2026-06-10T21:14:17+00:00

I’m trying to write a library that will programmatically add around_update / around_destroy callbacks

  • 0

I’m trying to write a library that will programmatically add around_update/around_destroy callbacks to a ActiveRecord model.

So, a regular model would look like this and it works as expected:

class User < ActiveRecord::Base
  around_update :test_update

  def test_update
    Rails.logger.debug "test_update"
    yield
    Rails.logger.debug "Finished test_update"
  end
end

u=User.last
u.name = 'something'
u.save

######### output (as expected):
# test_update
# Finished test_update

My little library (obviously just the skeleton) looks like this:

# A module for creating around callbacks in a model
module Piddle
  module TimelineFor
    def self.included(klass)
      klass.send(:extend, ClassMethods)
    end

    module ClassMethods
      def timeline_for(event, opts={})
        method_name = :"timeline_for_#{event.to_s}"
        define_method(method_name) do |&block|
          Rails.logger.debug method_name.to_s
          yield block
          Rails.logger.debug "After yield in #{method_name.to_s}"
        end

        send(:around_update, method_name)
      end
    end
  end
end

It defined a timeline_for method which should add the timeline_for_update method and make that a callback for the around_update event. And the User model I want to use is this:

# second version of the User model using Piddle to create the callback
require 'piddle/piddle'

class User < ActiveRecord::Base
  include Piddle::TimelineFor

  timeline_for :update
end

u=User.last
u.name = 'gfgfhfhfgh'
u.save

In the output I see

timeline_for_update
LocalJumpError: no block given (yield)
from /vagrant/lib/piddle/piddle.rb:13:in `block in timeline_for'

The first output line indicates that the method is being called but the block isn’t being passed in.

Any ideas or alternative implementations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:14:19+00:00Added an answer on June 10, 2026 at 9:14 pm

    The problem is that if you call yield from your define_method, ruby interprets that as trying to yield to the (nonexistant) block that was passed to timeline_for, not the block
    that rails passed to timeline_for_foo

    You’ve got block being passed to you so you can just call it:

    def timeline_for event
      method_name = "timeline_for_#{event}"
      define_method method_name do |&block|
        ActiveRecord::Base.logger.debug "before #{method_name} yield" 
        block.call
        ActiveRecord::Base.logger.debug "after #{method_name} yield" 
      end
      send :around_update, method_name.to_sym #must use a symbol here
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.