This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Adding Hooks to Table Events
CodeExpectedActual
require 'cookbook_dbconnect'
require 'og'
require 'glue/aspects'
class BlogPost
  property :title, :content, String
  after :on => :og_insert do |post|
    puts %{Sending email notification of new post "#{post.title}"}
    # Actually send the email here...
  end
end
og_connect
post = BlogPost.new
post.title = 'Robots are taking over'
post.content = 'Think about it! When was the last time you saw another human?'
post.save!
Sending email notification of new post "Robots are taking over"
Sending email notification of new post "Robots are taking over"
require 'cookbook_dbconnect'
activerecord_connect
class BlogPost < ActiveRecord::Base
  def after_create
    puts %{Sending email notification of new blog post "#{title}"}
    # Actually send the email here...
  end
end
post = BlogPost.create(:title => 'Robots: Gentle Yet Misunderstood',
                       :content => 'Popular misconceptions about robERROR 40')
Sending email notification of new blog post "Robots: Gentle Yet Misunderstood
Error! (Exception?) Here's stdout:
TypeError: superclass mismatch for class BlogPost
	from (irb):18
	from :0
ArgumentError: wrong number of arguments (1 for 0)
	from /usr/lib/ruby/gems/1.8/gems/og-0.29.0/lib/og/entity.rb:123:in `initialize'
	from /usr/lib/ruby/gems/1.8/gems/og-0.29.0/lib/og/entity.rb:123:in `create'
	from (irb):24
	from :0
require 'cookbook_dbconnect'
activerecord_connect
class BlogPost < ActiveRecord::Base
end
class MailObserver < ActiveRecord::Observer
 observe BlogPost
  def after_create(post)
    puts %{Sending email notification of new blog post "#{post.title}"}
    # Actually send the email here.
  end
end
ActiveRecord::Base.observers = MailObserver
post = BlogPost.new(:title => "ERROR 40",
                    :content => "ERROR ERROR ERROR ERROR ERROR")
post.save
Sending email notification of new blog post "ERROR 40"
environment.rb
TypeError: superclass mismatch for class BlogPost
	from (irb):28
	from :0
NoMethodError: undefined method `add_observer' for BlogPost:Class
	from /usr/lib/ruby/gems/1.8/gems/og-0.29.0/lib/og/entity.rb:477:in `method_missing'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/observer.rb:117:in `initialize'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/observer.rb:116:in `initialize'
	from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `new'
	from /usr/lib/ruby/1.8/singleton.rb:95:in `instance'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/observer.rb:25:in `observers='
	from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/observer.rb:22:in `observers='
	from (irb):37
	from :0
ArgumentError: wrong number of arguments (1 for 0)
	from (irb):38:in `initialize'
	from (irb):38
	from :0
config.active_record.observers = MailObserver
...
NameError: undefined local variable or method `config' for #<Object:0xb7d66970>
	from (irb):41
	from :0