Skip to content

Instantly share code, notes, and snippets.

@moro
Created October 23, 2013 05:20
Show Gist options
  • Save moro/7112972 to your computer and use it in GitHub Desktop.
Save moro/7112972 to your computer and use it in GitHub Desktop.
場当たり的なコールバック回避
class Foo < ActiveRecord::Base
after_save :do_something, unless: :other_situation?
class << self
def other_situation
begin
Thread.current[:other_situation] = true
yield
ensure
Thread.current[:other_situation] = nil
end
end
end
def do_something
end
def other_situation?
Thread.current[:other_situation]
end
end
Foo.other_situation do
foo.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment